Functions chapter 3 of the text int main(void) double x,y,z; x = cube(y/3.0); printf( %f cubed is %f,x,cube(x)); return 0; return parameter type double cube(double var) return (var*var*var); main() is the caller cube() is the callee main() calls invokes cube() 2 times cube() is called by main() 2 times from 142 F -1
Picturing call and return main() call return call return cube() cube() Can simplify and complete the picture: Operating System main() Static Call Graph: gives the call hierarchy, who calls who cube() printf() 142 F -2
void (1) as a return type The function returns NO result void display_salary(double salary) printf( You earn $%.2f,salary); return /*Optional*/ Write display_salary(mysalary); to call the function 142 F-3
void (2) as a parameter The function takes NO parameter int number(void) int user_number; printf("enter a number: "); scanf("%i",&user_number); return user_number; Write number(); to call the function as a parameter and return type void welcome(void) printf("welcome"); Write welcome(); to call the function 142 F-4
Marching order: Control Flow (order of execution of a program) Begin at main() (no matter where main is) Execute each statement in descending order (top to bottom) UNLESS: function call function return if : Some statements can be skipped loops: some statements can be repeated (see later) 142 F -5
Control Flow: Example CODE void prompt (void) printf("next integer:"); int main (void) prompt(); prompt(); return 0; EXECUTION main prompt( ); prompt( ); prompt printf prompt printf 142 F -6
Functions Prototypes To declare a function Put the prototypes after the # commands e.g. double tax(double income, double rate); The names are arbitrary and can even be omitted. But use them to clarify your program. A prototype specifies how the function is called. The compiler checks any call to a function against the prototype and generates an error if there is a mismatch amount = tax(30000.0); /*error*/ need another argument of type double 142 F-7
Note: when writing code, you can just copy and paste the first line of your function definitions for the function prototypes. #include <stdio.h> /* prototypes */ double tax(double income, double rate); /* function definitions */ int main(void) amount = tax(30000.0, 0.12); return 0; double tax(double income, double rate) /*compute and return the tax */ return income*rate; 142 F-8
Parameters The caller may need to pass the function some values on which to operate input parameters of the function Inputs are specified as formal parameters in function definition double cube(double var) formal input parameter return (var*var*var); When calling the function, the caller provides actual parameters the value of the actual parameter is substituted for the formal parameter int main(void) x=cube(z/2); parameter passing double cube(double var) return (var*var*var); 142F-9
Control and Data Flow When a function is called: (1) control (=execution) transfers to the function body (2) the function executes (3) control returns to the point of call int main(void) double x,y,z; y=6.0; x=cube(y); 216.0 return 0; double cube(double var) 2 return(var*var*var); Note: functions are called by value. var and y are in different locations of the memory. When calling cube, the value of y is copied into var (more on this later). 3 1 6.0 142F-10
Multiple parameters A function can have multiple parameters. The actual parameters (in the call statement) must match the formal parameters (in the header of the function definition) in number, order and type /* in main */ double length_room, width_room; double surface; surface = area(length_room,width_room); match in type, number, and order but can have different names /* definition of area */ double area(double length, double width) return (length*width); 142F-11
Rules for using functions actual parameters MUST match formal parameters in number in order in type a function can ONLY return ONE value In a function that returns type T, the return expression MUST be of type T A function with return type T can be used anywhere an expression of type T can be used. 142F-12
Why have functions? Reuse of program text code it once and use it many times saves space and improve correctness Centralize changes changes and bug fixes are made in one place Better program organization easier to test, understand, and debug Modularization for team projects each person can work independently BETTER: see the forest, and not just the trees The program is shaped in meaningful units. 142F-13
Why have functions? This is how modern programming is done: API Application Programming Interface A library of functions for a particular purpose e.g. Windows API. 142F-14