Presentation is loading. Please wait.

Presentation is loading. Please wait.

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.

Similar presentations


Presentation on theme: "Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions."— Presentation transcript:

1 Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions

2 Etter/Ingber Modularity

3 Etter/Ingber Modularity 4 Execution of a program begins in the main function 4 The main function can call other functions –Functions defined in the same file –Function defined in other files or libraries 4 Functions are also referred to as modules 4 A module is a set of statements that performs a task or computes a value

4 Etter/Ingber Advantages of using modules 4 Modules can be written and tested separately 4 Large projects can be developed in parallel 4 Reduces length of program, making it more readable 4 Promotes the concept of abstraction

5 Etter/Ingber Programmer Defined Functions

6 Etter/Ingber Functions 4 Defined to –return a single value to the calling function –perform a task –change the value of the function arguments (call by reference)

7 Etter/Ingber Functions 4 Pre-defined –standard libraries 4 Programmer defined

8 Etter/Ingber Pre-defined Functions Example #include int main(void) { double angle; printf( input angle in radians: \n); scanf(%1f, &angle); printf( \nthe sine of the angle is %f\n,sin(angle) ); return 0; }//end main

9 Etter/Ingber Programmer Defined Functions Terminology 4 Function Prototype –describes how a function is called 4 Function Call 4 Actual parameter –used in the function call 4 Function Definition 4 Formal Parameters –used in function definition 4 Formal parameters must match with actual parameters in order, number and data type.

10 Etter/Ingber Value Returning Functions 4 Function returns a single value to the calling program 4 Function definition declares the type of value to be returned 4 A return expression; statement is required in the function definition

11 Etter/Ingber Example - factorial function /*function definition n! = n*(n-1)*(n-2)*…*1, 0! = 1 by definition - fact returns n! assumes n is non-negative integer */ int fact(int n) { int fact = 1; while(n>1) { fact = fact*n; n--; }//end while block return(fact); }//end fact

12 Etter/Ingber Function prototype - prototype can be included with preprocessor directives, or with variable declarations. #include int main(void) { /* Declare variables and function prototypes. */ int n; int fact(int n); printf(Enter a positive integer\n); scanf("%i, &n); if(n>=0) printf(%i! is %i\n, n, fact(n) ); return 0; } Note: In this example the function fact is called in the printf statement.

13 Etter/Ingber Calling a function - the value returned by a function can be assigned to a variable, printed, or used in an expression #include int main() { /*Declare variables and function prototypes */ int fact(int); int n, factorial; printf(enter positive integer\n); scanf(%lf,&n); if(n>=0) { factorial = fact(n); printf(%i! is %i\n, n, factorial); } return 0; }

14 Etter/Ingber void Functions 4 A void function may be called to perform a particular task (clear the screen) modify data perform input and output 4 A void function does not return a value to the calling program 4 if a return; statement is used (no return value)

15 Etter/Ingber Example of void function definition void print_date(int mo, int day, int year) { /*output formatted date */ printf(%i%i%i\n, mo, day, year ); return; }

16 Etter/Ingber Parameter Passing 4 Call by value –formal parameter receives the value of the actual parameter –function can not change the value of the actual parameter (arrays are an exception) 4 Call by reference –actual parameters are pointers (pointers will be discussed in chapter 6)

17 Etter/Ingber Storage Class and Scope 4 Scope refers to the portion of the program in which it is valid to reference a function or a variable 4 Storage class refers to the lifetime of a variable

18 Etter/Ingber Scope 4 Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it 4 Global scope - a global variable is defined outside the main function and can be accessed by any function within the program file.

19 Etter/Ingber Storage Class - 4 types 4 automatic - key word auto - default for local variables –Memory set aside for local variables is not reserved when the block in which the local variable was defined is exited. 4 external - key word extern - used for global variables –Memory is reserved for a global variable throughout the execution life of the program. 4 static - key word static –Requests that memory for a local variable be reserved throughout the execution life of the program. The static storage class does not affect the scope of the variable. 4 register - key word register –Requests that a variable should be placed in a high speed memory register.


Download ppt "Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions."

Similar presentations


Ads by Google