1 / 24

FUNCTIONS IN C – ELEMENTARY PROGRAM STRUCTURE

FUNCTIONS IN C – ELEMENTARY PROGRAM STRUCTURE. Extra slides. Passing arguments (parameters). #include &lt;stdio.h&gt; /* doesn't do what we want */ void mystery(int i, int j) ; int main(void) { int i,j; i=10; j=20; printf(&quot;Before mystery, i = %d and j = %d<br>&quot;,i,j); mystery(i,j);

jargo
Download Presentation

FUNCTIONS IN C – ELEMENTARY PROGRAM STRUCTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FUNCTIONS IN C – ELEMENTARY PROGRAM STRUCTURE Extra slides Chapter 4 Slides

  2. Passing arguments (parameters) #include <stdio.h> /* doesn't do what we want */ void mystery(int i, int j) ; int main(void) { int i,j; i=10; j=20; printf("Before mystery, i = %d and j = %d\n",i,j); mystery(i,j); printf("After mystery, i = %d and j = %d\n", i,j); } Chapter 4 Slides

  3. Passing arguments (parameters) void mystery(int i,int j) { int temp; temp = i; i = j ; j = temp ; } The intention of the function mystery is to swap the values of the two variables. However, the output is: Before mystery, i = 10 and j = 20 After mystery, i = 10 and j = 20 Chapter 4 Slides

  4. Why didn’t the swap work? • In this program, parameters are passed to the function mystery() • The swap occurs in mystery() as it should. • The values that were given to the function mystery( ) were copies of i and j. Chapter 4 Slides

  5. What about the arguments? • A copy of each argument was passed to mystery() • The function mystery() manipulated the copies. • The copies were destroyed after returning from mystery(), leaving the originals unchanged. • Using copies is called passing arguments (or parameters) by value. Chapter 4 Slides

  6. How can we get around this copying mechanism? • One way is to use “global variables.” Their scope is everything in the file from the point of declaration forward. • Try to avoid global variables, since they can get confused with other variables. Chapter 4 Slides

  7. Example of use of globals /* solution using global variables */ #include <stdio.h> int i, j; void mystery(void); int main(void) { i=10; j=20; mystery(); printf("After mystery, i = %d and j = %d\n",i,j); } Chapter 4 Slides

  8. Example of use of globals /* mystery has no parameters */ void mystery(void) { int temp; temp = i; globals from previous declaration i = j ; j = temp ; } Chapter 4 Slides

  9. Example of use of globals /* mystery has no parameters */ void mystery(void) { int temp; temp = i; globals from previous declaration i = j ; Note - these are not seen on this page j = temp ; } Chapter 4 Slides

  10. Are global variables used often? • In long source code files, it is hard to remember where global variables are. • The problem is worse for programs that are split over multiple files (which we will do later in this course). • Try to avoid global variables, since they can get confused with other variables. Chapter 4 Slides

  11. Review of function calls • C functions can easily call other C functions. • When a function is called, copies made of each argument are made and execution begins in the function. • The called function continues execution until either a return statement is reached or the function is exited because no more statements are to be executed. • The compiler places copies of parameters are placed on a stack and they are popped off the stack, last-in–first-out, when they are used. • Think of a stack of trays in a cafeteria. Chapter 4 Slides

  12. Recursion • A special case: functions can call themselves. • This calling process can go on until there is a way to exit the function. Chapter 4 Slides

  13. Recursion #include <stdio.h> int fact(int); int main(void) { int i; printf("Please enter a positive integer\n"); scanf("%d",&i); printf("The factorial of %d is %d\n",i,fact(i)) ; } Chapter 4 Slides

  14. Recursion int fact(int i) { if (i <=1) return 1; here’s the recursion else return (i*fact(i-1)); } Chapter 4 Slides

  15. Notice the environment level • In main(), environment level is 1 • This will change with each call (increment) • This will change with each return (decrement) • Goes up 1 on each call printf(), scanf() • When we call second printf(), environment level is 2 • When we call fact(), environment level is 3 Chapter 4 Slides

  16. First call to fact, say, with an argument of 4 int fact(4) { if (4 <=1) return 1; else return (i*fact(4-1)); } Stack: push data Env. Level is 3 Name arg value fact() 4 ? printf() … main() … Here’s how the stack works Chapter 4 Slides

  17. Second call to fact, with an argument of 3 int fact(3) { if (3<=1) return 1; else return (i*fact(3-1)); } Stack: push data Env level is 4. Name arg value fact() 3 ? fact() 4 ? Here’s how the stack works Chapter 4 Slides

  18. Third call to fact, with an argument of 3-1 int fact(2) { if (2 <=1) return 1; else return (i*fact(2-1)); } Stack: push data Env level is 5 Name arg value fact() 2 ? fact() 3 ? fact() 4 ? printf() … main() … Here’s how the stack works Chapter 4 Slides

  19. Last call to fact, with an argument of 1 int fact(1) { if (1 <=1) return 1; else return (i*fact(1-1)); } Stack: push data Env level is 6 Name arg value fact() 1 ? fact() 2 ? fact() 3 ? fact() 4 ? printf() … main() … Here’s how the stack works Chapter 4 Slides

  20. Last call to fact, with an argument of 1 int fact(1) { if (1 <=1) return 1; else return (i*fact(1-1)); } Stack: change the top Env level is 6, but will change with return Name arg value fact() 11 fact() 2 ? fact() 3 ? fact() 4 ? printf() … main() … Now we have return values Chapter 4 Slides

  21. Prev. call to fact, with an argument of 2 int fact(2) { if (2 <=1) return 1; else return (i*fact(2-1)); } Stack: pop the top Env level is 5, but will change with return Name arg value fact() 22 * 1 fact() 3 ? fact() 4 ? printf() … This value is known! It is 1, from previous call to fact at environment level 6 Return first value Chapter 4 Slides

  22. Prev. call to fact, with an argument of 3 int fact(3) { if (3 <=1) return 1; else return (i*fact(3-1)); } Stack: pop the top Env level is 4, but will change with return Name arg value fact() 33 * 2 fact() 4 ? printf() … This value is known! It is 2, from previous call to fact at environment level 5 Return first value Chapter 4 Slides

  23. Prev. call to fact, with an argument of 4 int fact(4) { if (4 <=1) return 1; else return (i*fact(4-1)); } Stack: pop the top Env level is 4, but will change with return Name arg value fact() 34 * 6 printf() … main() … This value is known! It is 6, from previous call to fact at environment level 4 Return first value Chapter 4 Slides

  24. Return from fact, with an argument of 4 int fact(4) { if (4 <=1) return 1; else return (i*fact(4-1)); } Stack: pop the top Env level is 3, but will change with return Name arg value printf() … 24 main() … This value is known! It is 24, from previous call to fact at environment level 3 Return first value Chapter 4 Slides

More Related