Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Programming Using C Modularity Contents 2 Modularity Functions Preprocessor Comments Global variables Modularity As programs get bigger, they get harder to manage This happens with most things and we have developed ways to handle it – – – 3 Too many things – put them in baskets Need to play several songs – put them in a playlist Got a tough problem – break it into a series of simpler problems Modularity The C language lets us split our code into parts called functions A function is one way to create a module A module is simply a part of a larger program A module – – 4 Encapsulates part of a program Can be invoked many times Functions A function – – – – – 5 Is a block of code Has a name Has a series of parameters which can be passed to it Can return a value Can be invoked by other code The Function piTimes A simple function to multiply a number times Type returned Function Body Function name Parameter list double piTimes(double n) { return n * 3.141592654; } Indicates value to return 6 Functions Every function has – – – – 7 A name which is used to invoke it and must be unique A parameter list of values passed to the function, which might be empty A return type, indicating the type of value returned A body which performs some calculation using the values from the parameter list and returns a result An Interest Calculator We want to calculate interest – – – We will show – – 8 On a give amount of money At a specific interest rate For a given number of years How to write a function to do this How to invoke this function An Interest Calculator double interest(double principal, double rate, int time) { int i; for(i = 1; i <= time; i = i + 1) principal = principal * (1 + rate / 100); return principal; } 9 An Interest Calculator Points of interest – – – – 10 3 parameters are passed to the function A local variable, i, is declared within the function A loop is used to calculate the result The result is returned via the return statement Invoking the Calculator main() { double start, end, rate; int years; printf(“Enter amount to invest or 0 to stop\n”); scanf(“%lf”, &start); while(start > 0) { printf(“Enter rate\n”); scanf(“%lf”, &rate); printf(“Enter years to invest\n”); scanf(“%d”, &years); end = interest(start, rate, years); printf(“Your investment will be worth %.1lf\n”, end); printf(“Enter amount to invest or 0 to stop\n”); scanf(“%lf”, &start); } } 11 Compilation The C language uses a one-pass compiler – – We can overcome this problem by – 12 This means that it reads the code once only from start to finish If you call a function before it sees the function, it will say the function does not exist Using forward declarations for every function in the program at the start of the program Forward Declarations Forward declarations – Use the prototype of the function to tell the compiler A prototype is – 13 The name of the function The types of the parameters for the function The return type of the function The function header without the body of the function Forward Declarations The forward declaration for the interest function would be – double interest(double principal, double rate, int time); – This is simply the function header without the body Notice the semi-colon at the end of the line – 14 * See functiondemo.c The Preprocessor Before a C program is compiled it is run through a preprocessor The preprocessor – Looks for directives starting with # and – 15 Textually includes other files Remembers the definitions of macros Conditionally includes or excludes code from compilation Expands defined macros to their real values The Preprocessor C Source File C Preprocessor Modified C Source File C Compiler Object File Executable File Linker Library 16 The #include Directive The first preprocessor directive we will examine is the #include directive It has the format – #include <filename> – 17 Or #include “filename” These both textuall include another file into your program, replacing the directive The #include Directive The first form – – – The second form – – 18 #include <filename> Searches for the file name on the include path Think of it as searching in the system libraries for the file name you specified #include “filename” Searches for the file relative to the current directory The #include Directive Both printf and scanf are just regular functions Have you wondered how the compiler knows about them? We include their prototypes from what is called a header file – 19 #include <stdio.h> This file contains the prototypes of many common input and output functions Comments Comments are notes inserted into programs to explain to humans what the code is doing Comments are enclosed between /* and */ – 20 /* this is a comment */ Comments can cover several lines Comments are removed by the preprocessor and are never seen by the compiler It is good practice to comment your programs Typical Program Layout Comment describing program and author #include directives for standard functions Prototypes of functions other than main() Definition of the main() function Defintion of remaining functions with a comment for each 21 Sample Program /*****************************************************************/ /* Display table of investment profits at different rates. */ /*****************************************************************/ #include <stdio.h> double interest(double principal, double rate, int time); main() { double percent; printf(“Comparison of 5 year returns at different rates\n”); printf(“ Rate Profit per thousand $\n”); printf(“ ----------------------------------\n”); for(percept = 3.5; percent < 9.6; percent = percent + 0.5) printf(“ %.2lf %.2lf\n”, percent, interest(1000.0, percent, 5) – 1000); } /*************************************************************************/ /* Calculate value of principal after time years at rate percent */ /*************************************************************************/ double interest(double principal, double rate, int time) { int i; 22 for(i = 1; i <= time; i = i + 1) principal = principal * (1 + rate / 100); return principal; } Main main is a function, but … – – – – The full prototype for main is – – – 23 It is the first function called in your program It is called by the operating system It can communicate with the operating system It has a default return type and optional parameter list int main(char *argv[]) Technically, we should return 0 from main We won’t be using it to communicate with the O/S in this course Global Variables We can declare variables in several places – At the top of the file, outside any function – At the start of a function – This is a local variable, visible only within the function Within a set of curly brackets inside a function 24 This is a global variable, visible everywhere This is a variable visible only within the curly brackets in which it is declared Global Variables The scope of a variable is the amount of code which can see the variable Global variables – Local variables – Can be see throughout a function Block variables – 25 Can be seen by any code in the file Can be see throughout the block in which they are declared Global Variables Many students think global variables are handy because – – You don’t have to pass parameters because they are visible in every function You only declare them once However, this is not the case because – – It is very hard to find the functions which modify the global variables This makes it 26 Harder to understand how the program works Harder to find mistakes in the program