APS105 Functions (and Pointers) Functions Tetbook Chapter5 1 2 Modularity Modularity Break a program into manageable parts (modules) Modules interoperate with each other Benefits of modularity: Divide-and-conquer: work on small problems Small parts are easier to read/understand Different people can work on different parts Avoid duplication of the same code Re-use modules from other programs Can test each module in isolation C pre-defined functions Math functions Sqrt(), fabs(), etc I/O functions printf(), scanf() modularity/modules in C: functions 3 4 1
Two Types of Functions Commands perform a process, but don t return anything Queries evaluate something, return a result Some languages: commands: called procedures, subroutines queries: called functions C language: commands: called functions queries: called functions Defining a Function Define a function that prints a blank line void skipline (void) printf("\n"); Note: this is called a "function definition" 5 6 Incorporating a Function in a Program #include <stdio.h> void skipline (void) printf("\n"); skipline(); skipline(); Function Prototype Sometimes must summarize a function tell the compiler what it needs to know eg., if function has parameters or return value Function Prototype: similar to a copy of the first line of the function definition Eample of a prototype: void skipline (void); 7 8 2
Prototype Eample: Declare First #include <stdio.h> void skipline (void); skipline(); skipline(); a prototype here solves the problem compiler needs to know if this is the correct way to call skipline(), but it hasn't seen its definition yet Functions with Parameters Define a function that prints n blank lines void skiplines (int n) for (int i = 1; i <= n; i++) printf("\n"); void skipline (void) printf("\n"); Note: eecution always starts with regardless of the order functions appear in the file 9 Note: n is called a "parameter" 10 Calling a Function with Parameters #include <stdio.h> void skiplines (int); int i = 10; skiplines(i+5); void skiplines (int n) need only provide the type of each parameter i+5 is called an "argument" When A Function is Called 1) Arguments are evaluated 2) control passes to the function 3) parameters are assigned values the values of the arguments are copied i.e., param gets a copy of the value of the arg this method is called "call-by-value" the only method used by C other languages provide other methods 4) the function is eecuted 5) control passes back to the calling function 11 12 3
Eample of A Query Function a function that returns the factorial of an int int factorial (int n) // 5 copied to n int product = 1; for (int i=1; i <= n; i++) product *= i; return product; // returns 120 int int = 2, y; y = factorial( + 3); // called with 5 // 120 copied to y 13 Animation of calls foo calls bar int = foo(4); int = foo(4); 4
int = foo(4); int = foo(4); int = foo(4); foo y 4 int = foo(4); foo y 4 5
int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo bar z 4 y 4 = 11 int = foo(4); foo y 4 6
= 15 int = foo(4); foo y 4 = 15 int = foo(4); foo y 15 int = foo(4); = 15 int = foo(4); 15 7
int = foo(4); 15 Function Scope of Identifiers/Variables An identifier/variable eists within its scope if declared within a function the scope is within the function ( local variable ) Implications: for a variable declared within a function can't be accessed outside that function ( local ) can re-use the identifier in multiple functions Be careful, as this can create confusion with variables with the same name Remember What happens in Vegas, stays in Vegas. 30 Eample of Scope // 7 copied to fun:n // fun:n = 3; // returns 3 Animation of scope // :n set to 7 // fun called // 3 copied to result 31 8
result result 9
fun fun fun result result n 3 result = 3 n 3 result 10
fun = 3 n 3 result = 3 result result 3 result 3 11
Eample of Multiple Parameters int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); Animation of Multipe Parameters parameters copied in left to right order 45 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 12
sum3 sum3 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 4 y 5 z 6 int sum3 (int, int y, int z) return + y + z; = 15 int = sum3(4,5,6); 4 y 5 z 6 13
sum3 int sum3 (int, int y, int z) return + y + z; = 15 int = sum3(4,5,6); 4 y 5 z 6 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); = 15 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); = 15 15 int sum3 (int, int y, int z) return + y + z; int = sum3(4,5,6); 15 14
Parameter Type Correspondence Param and argument types needn't match as long as they are "assignment compatible" Eample: double power (double, int n); result = power(5, 2); // 5 cast to 5.0 Returning result = power(5.3, 2.9); // 2.9 cast to 2 57 58 Multiple Returns A function can have multiple return statements Eample: a function that returns ma value int ma(int, int y) if ( > y) return ; return y; 59 No Return Eample: print n blank lines void skiplines(int n) if (n < 1) return; // ok to have this for (int i = 1; i < n; i++) printf("\n"); return; // optional to have this multiple returns OK for a command function 60 15
Style: Returning Bool Functions that return bool should be named: is<something> Produces more readable code Eample: if (isperfectsquare())... isperfectsquare bool isperfectsquare(int ) if ((int)sqrt()*(int)sqrt() == ) return true; else return false; Having multiple returns is bad style for a queryfunction 61 62 isperfectsquare (cont'd) bool isperfectsquare(int ) bool result = false; if ((int)sqrt()*(int)sqrt() == ) result = true; return result; better style---can we be more concise? isperfectsquare (cont'd) bool isperfectsquare(int ) return (int)sqrt()*(int)sqrt() == ; bool isperfectsquare(int ) int sqrt_of_ = (int) sqrt(); return sqrt_of_ * sqrt_of_ == ; 63 64 16