Department of Computer and Information Science School of

  • Slides: 33
Download presentation
Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Functions Dale

Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Functions Dale Roberts, Lecturer IUPUI droberts@cs. iupui. edu Dale Roberts

Introduction Functions Category Programmers Trivial 1 homework) Small 1 -3 projects) Medium 2 -5

Introduction Functions Category Programmers Trivial 1 homework) Small 1 -3 projects) Medium 2 -5 project) Large 5 -25 application) Very Large 25 -100 time operations) Extremely Large > 100 military work) Duration Size of Code 1 -2 weeks < 500 lines (student Few Weeks 500 -2000 lines (term Few Months 2000 -10000 (research 1 - 3 years 10, 000 - 100, 000 (current 3 - 5 years 100, 000 - 1 M (real- > 5 years >1 M (advanced Divide and conquer Large programs cannot be monolithic Construct a program from smaller pieces or components. These smaller pieces are called modules Each piece is more manageable than the original program Dale Roberts

Program Modules in C Functions Modules in C Programs combine user-defined functions with library

Program Modules in C Functions Modules in C Programs combine user-defined functions with library functions C standard library has a wide variety of functions Math function, I/O, string function, such as printf(), scanf() Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Write function once and call it many times Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details Dale Roberts

Math Library Functions Math library functions perform common mathematical calculations #include <math. h> Format

Math Library Functions Math library functions perform common mathematical calculations #include <math. h> Format for calling functions Function. Name( argument ); If multiple arguments, use comma-separated list printf( "%. 2 f", sqrt( 900. 0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions Dale Roberts

Functions Modularize a program All variables declared inside functions are local variables Known only

Functions Modularize a program All variables declared inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition Dale Roberts

Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements }

Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void – indicates that the function returns nothing An unspecified return-value-type is always assumed by the compiler to be int Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter, unless the parameter is of type int Declarations and statements: function body (block) Variables can be declared inside blocks (can be nested) Functions can not be defined inside other functions Returning control If nothing returned return; or, until reaches right brace If something returned return expression; Dale Roberts

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /* Fig. 5. 4: fig 05_04. c Finding the maximum of three integers */ #include <stdio. h> int maximum( int, int ); /* function prototype */ 1. Function prototype (3 parameters) int main() { int a, b, c; printf( "Enter three integers: " ); scanf( "%d%d%d", &a, &b, &c ); printf( "Maximum is: %dn", maximum( a, b, c ) ); return 0; } 2. Input values 3. Call function 4. Function definition /* Function maximum definition */ int maximum( int x, int y, int z ) { int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max; } Enter three integers: 22 85 17 Maximum is: 85 Program Output Dale Roberts

Function Prototypes Function prototype Function name Parameters – what the function takes in Return

Function Prototypes Function prototype Function name Parameters – what the function takes in Return type – data type function returns (default int) Used to validate functions Prototype only needed if function definition comes after use in program The function with the prototype int maximum( int, int ); Takes in 3 ints Returns an int Promotion rules and conversions Converting to lower types can lead to errors Dale Roberts

Examples plus 2(int x, y) float plus 1(float x, y) { int sum; {

Examples plus 2(int x, y) float plus 1(float x, y) { int sum; { sum = x + y; float sum; return sum; sum = x + y; } return sum; • }The minimal function is dummy(){} • Again, the return statement can be return expression; Example: return a*a; return (expression); Example: return ((a > 0)? a, -a)); return; • In conclusion, to call a function from another function, you should: 1. Declare the type of the called function, if not a int function 2. Use format of function_name(actual parameters) Dale Roberts

More Examples: Example 1: … float my. Square(float); main() { float y; printf(%fn, my.

More Examples: Example 1: … float my. Square(float); main() { float y; printf(%fn, my. Square(5. 0)); } Old style float my. Square(float x) { return x*x; } float my. Square(x) float x; { return x*x; } Example 2: /* calculate the sum from 1+2+…+n */ int sum(int n) { int i, sum = 0; for (i=1; i<=n; i++) sum += i; return(sum); } How can we have both a function and a variable named “sum? ” main() { int j, n; printf(“input value n: “); scanf(“%d”, &n); for (j=1; j<=n; j++) printf(“sum from 1 to %d is %dn”, j, sum(j)); } Dale Roberts

Header Files Header files Contain function prototypes for library functions <stdlib. h> , <math.

Header Files Header files Contain function prototypes for library functions <stdlib. h> , <math. h> , etc Load with #include <filename> #include <math. h> Custom header files Create file with functions Save as filename. h Load in other files with #include "filename. h" Reuse functions Dale Roberts

Calling Functions: Call by Value and Call by Reference Used when invoking functions Call

Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument Avoids accidental changes Example: addone(int); main () { int i = 5, j; j=addone(i); printf(“%d %dn”, i, j); } 5 6 addone(int x) { return ++x; } Dale Roberts

Calling Functions: Call by Value and Call by Reference Call by reference (Passing Address)

Calling Functions: Call by Value and Call by Reference Call by reference (Passing Address) This is not actually call by reference, although some books called this ‘call-by-reference’ Passes original argument Changes in function effect original using & operator to pass address Only used with trusted functions Example: addone(int); main () { } int i = 5, j; j=addone(&i); printf(“%d %dn”, i, j); 6 6 addone(int *x) { return ++(*x); } For now, we focus on call by value Dale Roberts

Random Number Generation rand function Load <stdlib. h> Returns "random" number between 0 and

Random Number Generation rand function Load <stdlib. h> Returns "random" number between 0 and RAND_MAX (at least 32767) i = rand(); Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling To get a random number between 1 and n 1 + ( rand() % n ) rand() % n returns a number between 0 and n-1 Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6 Dale Roberts

Random Number Generation srand function <stdlib. h> Takes an integer seed and jumps to

Random Number Generation srand function <stdlib. h> Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); srand( time( NULL ) ); //load <time. h> time( NULL ) Returns the time at which the program was compiled in seconds “Randomizes" the seed Dale Roberts

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* Fig. 5. 9: fig 05_09. c Randomizing die-rolling program */ #include <stdlib. h> #include <stdio. h> int main() { int i; unsigned seed; printf( "Enter seed: " ); scanf( "%u", &seed ); srand( seed ); for ( i = 1; i <= 10; i++ ) { printf( "%10 d", 1 + ( rand() % 6 ) ); if ( i % 5 == 0 ) printf( "n" ); } 1. Initialize seed 2. Input value for seed 3. Use srand to change random sequence 4. Define Loop 5. Generate and output random numbers Program Output return 0; } Enter seed: 67 6 1 4 6 2 1 6 4 Enter seed: 867 2 4 6 1 1 3 6 2 Enter seed: 67 6 1 4 6 2 1 6 4 Dale Roberts

Example: A Game of Chance Craps simulator Rules Roll two dice 7 or 11

Example: A Game of Chance Craps simulator Rules Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4, 5, 6, 8, 9, 10 - value becomes player's "point" Player must roll his point before rolling 7 to win Example: 11 12 10, 6, 11, 6, 10 4, 5, 9, 10, 9, 3, 7 Dale Roberts

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /* Fig. 5. 10: fig 05_10. c Craps */ #include <stdio. h> #include <stdlib. h> #include <time. h> 1. roll. Dice prototype int roll. Dice( void ); int main() { int game. Status, sum, my. Point; srand( time( NULL ) ); sum = roll. Dice(); /* first roll of the dice */ switch ( sum ) { case 7: case 11: /* win on first roll */ game. Status = 1; break; case 2: case 3: case 12: /* lose on first roll */ game. Status = 2; break; default: /* remember point */ game. Status = 0; my. Point = sum; printf( "Point is %dn", my. Point ); break; } while ( game. Status == 0 ) { /* keep rolling */ sum = roll. Dice(); 2. Initialize variables 3. Seed srand 4. Define switch statement for win/loss/continue 5. Loop Dale Roberts

33 34 35 36 37 38 39 40 41 42 43 44 45 46

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 if ( sum == my. Point ) /* win by making point */ game. Status = 1; else if ( sum == 7 ) /* lose by rolling 7 */ game. Status = 2; } if ( game. Status == 1 ) printf( "Player winsn" ); else printf( "Player losesn" ); Program Output 6. Print win/loss return 0; } int roll. Dice( void ) { int die 1, die 2, work. Sum; die 1 = 1 + ( rand() % 6 ); die 2 = 1 + ( rand() % 6 ); work. Sum = die 1 + die 2; printf( "Player rolled %d + %d = %dn", die 1, die 2, work. Sum ); 56 return work. Sum; 57 } Player rolled 6 + 5 = 11 Player wins Player rolled 6 + 6 = 12 Player loses Player rolled 4 + 6 = 10 Point is 10 Player rolled 2 + 4 = 6 Player rolled 6 + 5 = 11 Player rolled 3 + 3 = 6 Player rolled 6 + 4 = 10 Player wins Player rolled 1 + 3 = 4 Point is 4 Player rolled 1 + 4 = 5 Player rolled 5 + 4 = 9 Player rolled 4 + 6 = 10 Player rolled 6 + 3 = 9 Player rolled 1 + 2 = 3 Player rolled 5 + 2 = 7 Player loses Dale Roberts

Storage Classes Storage class specifiers Storage duration – how long an object exists in

Storage Classes Storage class specifiers Storage duration – how long an object exists in memory Scope – where object can be referenced in program Linkage – specifies the files in which an identifier is known (more in Chapter 14) Automatic storage Object created and destroyed within its block auto: default for local variables and usually stored in Stack. auto double x, y; register: tries to put variable into high-speed registers Can only be used for automatic variables register int counter = 1; Example: f(register int c, register int n) { register int i; … } Dale Roberts

Storage Classes Static storage Variables exist for entire program execution Default value of zero

Storage Classes Static storage Variables exist for entire program execution Default value of zero static: local variables defined in functions. Keep value after function ends Only known in their own function; Example: static char buf[128]; static int bufp=0; getch 1() { … } Dale Roberts

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /* Fig. 5. 12: fig 05_12. c A scoping example */ #include <stdio. h> void a( void ); /* function prototype */ void b( void ); /* function prototype */ void c( void ); /* function prototype */ 1. Function prototypes int x = 1; /* global variable */ int main() { int x = 5; /* local variable to main */ printf("local x in outer scope of main is %dn", x ); 2. Initialize global variable 3. Initialize local variable { /* start new scope */ int x = 7; printf( "local x in inner scope of main is %dn", x ); } /* end new scope */ printf( "local x in outer scope of main is %dn", x ); 4. Initialize local variable in block a(); /* a has automatic local x */ b(); /* b has static local x */ c(); /* c uses global x */ a(); /* a reinitializes automatic local x */ 5. b(); /* static local x retains its previous value */ c(); /* global x also retains its value */ Call functions Dale Roberts

31 32 33 34 35 36 37 38 39 40 41 42 43 44

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 printf( "local x in main is %dn", x ); return 0; } void a( void ) 7. Function definitions { int x = 25; /* initialized each time a is called */ Program Output local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a void b( void ) local static x is 50 on entering b { local static x is 51 on exiting b static int x = 50; /* static initialization only */ /* first time b is called */ global x is 1 on entering c printf( "nlocal static x is %d on entering bn", x ); global x is 10 on exiting c ++x; printf( "local static x is %d on exiting bn", x ); local x in a is 25 after entering a local x in a is 26 before exiting a } local static x is 51 on entering b void c( void ) local static x is 52 on exiting b { printf( "nglobal x is %d on entering cn", x ); global x is 10 on entering c x *= 10; global x is 100 on exiting c printf( "global x is %d on exiting cn", x ); local x in main is 5 } printf( "nlocal x in a is %d after entering an", x ); ++x; printf( "local x in a is %d before exiting an", x ); } Dale Roberts

Scope Rules File scope Identifier defined outside function, known in all functions Used for

Scope Rules File scope Identifier defined outside function, known in all functions Used for global variables, function definitions, function prototypes Function scope Can only be referenced inside a function body Used only for labels (start: , case: , etc. ) Block scope Identifier declared inside a block Block scope begins at declaration, ends at right brace Used for variables, function parameters (local variables of function) Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block Function prototype scope Used for identifiers in parameter list Dale Roberts

extern Storage Class Specifier If an external variable is to be referred to before

extern Storage Class Specifier If an external variable is to be referred to before it is defined, or if it is defined in a different source file from the one where it is being used, then extern declaration is necessary extern: default for global variables and functions Known in any function Usage: if large number of variables must be shared among functions, external variables are more convenient and efficient than long argument list Example: In file 1: … int sp=0; double val[12]; … In file 2; extern int sp; extern double val[]; int push(f) double f; { … } … Dale Roberts

Recursion Recursive functions A function can invoke any other function; “ Can a function

Recursion Recursive functions A function can invoke any other function; “ Can a function invoke itself? ” Functions that call themselves either directly or indirectly (through another function) is called a recursive function. A recursive function is called to solve a problem. The function actually knows how to solve the simplest case(s) - “base case” and simply returns a result. If a function is called with a complex case, it divides the problem into two conceptual places: What it can do What it cannot do: 1. must resembles original problem but slightly simplified 2. function will call itself (recursion step or recursive call) to solve slightly simplified problem 3. process continues until the last recursive call is with the base case 4. that call returns the result of the base case 5. return to previous calling function 6. finally reaches main. c and goes back to 2. Eventually base case gets solved Gets plugged in, works its way up and solves whole problem Dale Roberts

Recursion (cont. ) Example: Compute x y for x > 0 & y >

Recursion (cont. ) Example: Compute x y for x > 0 & y > 0 xy=x*x*x*…*x y times base case: x 1 =x x 4 x 1 x 3 x y = x 1 * ( x * … * x) x 2 = x 1 * ( x * … * x )) = x 1 * ( x * … * x ))) ex. x 4 = x 1 * ( x 1 ))) x 1 Fundamental rules of recursion x 1 x 1 * x 3 625 54 51 53 51 * 125 * * 52 * x 2 25 51 51 x 2 x 1 * 5 5 1. ) Base cases 2. ) Making progress through recursion 3. ) Design rule: assuming all recursive call work (details hidden) 4. ) Compound interest rule: do not duplicate recursive calls Always specify the base case; otherwise, indefinite recursive will occur and cause “stack-overflow” error. Dale Roberts

Recursion vs. Iteration Recursion: Iteration: #include <stdio. h> int x_pow_y(int, int); main() { printf(“enter

Recursion vs. Iteration Recursion: Iteration: #include <stdio. h> int x_pow_y(int, int); main() { printf(“enter x and y: n”); scanf(“%d, %d”, x, y); z = x_pow_y(x, y); printf(“z: %dn”, z); } } x_pow_y(int a, int b) { if (b==1) return a; else return (a * x_pow_y(a, b-1)) … x=5; y=4; x_pow_y(5, 4); main x_pow_y(5, 4) … is (4==1)? 625 no x_pow_y = 1; for (i = y; i >=1; i--) x_pow_y*=x; If x = 5, y = 4 x_pow_y = 1, x = 5, y = 4, i = 4; 1. ) x_pow_y = 5*1 = 5, i = 3; 2. ) x_pow_y = 5*5 = 25, i = 2; 3. ) x_pow_y = 25*5 = 125, i = 1; 4. ) x_pow_y = 125*5 = 625, i = 0; x_pow_y(5, 3) … is (3==1)? 125 no x_pow_y(5, 2) … is (2==1)? 25 no x_pow_y(5, 1) 5 … is (1==1)? yes return 5 Base Case Dale Roberts

Example Using Recursion: Factorial Example: factorials: 5! = 5 * 4 * 3 *

Example Using Recursion: Factorial Example: factorials: 5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3! … Can compute factorials recursively Solve base case (1! = 0! = 1) then plug in 2! = 2 * 1 = 2; 3! = 3 * 2 = 6; long factorial(int n) { if (n <= 1) return 1; else return n * factorial(n-1); } Dale Roberts

Example Using Recursion: The Fibonacci Series Example: Fibonacci series: Fk = Fk-1 + Fk-2,

Example Using Recursion: The Fibonacci Series Example: Fibonacci series: Fk = Fk-1 + Fk-2, F 0 = 1, F 1 = 1 ex: 1, 1, 2, 3, 5, 8… Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) f( 3 ) Set of recursive calls to function fibonacci Code for the fibaonacci function return f( 1 ) long fibonacci(long n) { if (n <= 1) return 1; else; return fibonacci(n-1)+ fibonacci(n-2); + f( 2 ) + f( 0 ) f( 1 ) return 1 } Dale Roberts

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /* Fig. 5. 15: fig 05_15. c Recursive fibonacci function */ #include <stdio. h> long fibonacci( long ); int main() { long result, number; 1. Function prototype 2 Initialize variables printf( "Enter an integer: " ); 3. Input an integer scanf( "%ld", &number ); 4. Call function fibonacci result = fibonacci( number ); printf( "Fibonacci( %ld ) = %ldn", number, result ); return 0; 5. Output results. } /* Recursive definition of function fibonacci */ long fibonacci( long n ) 6. Define fibonacci { recursively if ( n == 0 || n == 1 ) return n; else return fibonacci( n - 1 ) + fibonacci( n - 2 ); } Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1 Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2 Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = 832040 Enter an integer: 35 Fibonacci(35) = 9227465 Dale Roberts

Recursion vs. Iteration Both are based on the control structures Repetition (Iteration): explicitly uses

Recursion vs. Iteration Both are based on the control structures Repetition (Iteration): explicitly uses repetition (loop). Selection (Recursion): implicitly use repetition by successive function calls Both involve termination Iteration: loop condition fails Recursion: base case recognized Both can lead indefinite loops Loop termination is not met Base case is not reached Balance Choice between performance (iteration) and good software engineering (recursion) Dale Roberts

Recursion vs. Iteration Any problem that can be solved recursively can also be solved

Recursion vs. Iteration Any problem that can be solved recursively can also be solved iteratively. A recursion is chosen in preference over iteration while the recursive approach more naturally mirrors the problem and results in a program that easier to understand debug. Recursion has an overhead of repeated function calls which is expensive in terms of processor time ad memory usage Another reason to choose recursion is that an iterative solution may not apparent. If used properly a recursive approach can lead to smaller code size and elegant program (at the case of performance penalty. ) Dale Roberts