Presentation is loading. Please wait.

Presentation is loading. Please wait.

142 F -1 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; } double cube(double.

Similar presentations


Presentation on theme: "142 F -1 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; } double cube(double."— Presentation transcript:

1 142 F -1 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; } double cube(double var) { return (var*var*var); } main() is the caller cube() is the callee main() calls cube() 2 times invokes return type parameter cube() is called by main() 2 times from

2 142 F -2 Picturing call and return main() cube() call return call return Can simplify and complete the picture: Operating System main() cube()printf() Static Call Graph: gives the call hierarchy, who calls who

3 void (1) 142 F-3 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

4 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 void (2) 142 F-4

5 142 F -5 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)

6 main {... prompt( );... prompt( ); } prompt { printf... } prompt { printf... } Control Flow: Example void prompt (void) { printf("Next integer:"); } int main (void) {... prompt();... prompt();... return 0; } CODE EXECUTION 142 F -6

7 142 F-7 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 need another argument of type double amount = tax(30000.0); /*error*/

8 142 F-8 when writing code, you can just copy and paste the first line of your function definitions for the function prototypes. Note: #include /* 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; }

9 Parameters 142F-9 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) { return (var*var*var); } formal input parameter 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);... } double cube(double var) { return (var*var*var); } parameter passing

10 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);... return 0; } double cube(double var) { return(var*var*var); } 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). Note: 216.0 3 6.0 1 2 142F-10

11 142F-11 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);... /* definition of area */ double area(double length, double width) { return (length*width); } match in type, number, and order but can have different names

12 142F-12 Rules for using functions actual parameters MUST match formal parameters in number in order in type a function can ONLY return ONE value A function with return type T can be used anywhere an expression of type T can be used. expression MUST be of type T In a function that returns type T, the return

13 Why have functions? 142F-13 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.

14 Why have functions? 142F-14 This is how modern programming is done: API Application Programming Interface A library of functions for a particular purpose e.g. Windows API.


Download ppt "142 F -1 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; } double cube(double."

Similar presentations


Ads by Google