1 / 22

Andy D. Digh Friday, May 29th, 1998

Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language. Andy D. Digh Friday, May 29th, 1998. Passing Function Arguments by Value.

reia
Download Presentation

Andy D. Digh Friday, May 29th, 1998

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. Passing Function Arguments by Reference :A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998

  2. Passing Function Arguments by Value So far, all the function calls we’ve seen have passed one or more arguments into a function using a method we call pass by value. Consider our call to function find_max: max = find_max (num1, num2);

  3. int find_max (int x, int y) /* Input : The values of two integers, x and y */ /* Output : Returns the larger of the two input values */ { int temp; if (x > y) temp = x; else temp = y; return temp; }

  4. Pictorially here showing the pass by value we have: In Our Main Program: Num1: 7 Num2: 4

  5. Now, once function find_max is called we have: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: ? x: 7

  6. Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: 7 x: 7

  7. Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: 7 Num2: 4 Max: ? Num1: In find_max: y: 4 Temp: 7 x: 7

  8. In Our Main Program: 7 7 Num2: 4 Max: Num1: Once our function returns our result, the function data area is always erased and will be recreated when the procedure is called again.

  9. Passing Function Arguments by Value • The functions we’ve seen with pass by value have also always returned a single value. • This has corresponded nicely to our understanding of functions from mathematics. F(x) = x2 + 3 F(2) = 22 + 3 = 7

  10. Passing Function Arguments by Value • They’ve also never modified a variable in the calling function. • But, sometimes we aren’t always interested in cranking out one result in our subprograms. That is, we want our subprograms to produce multiple values.

  11. Passing Function Arguments by Reference • In effect, we want to be able to alter a variable in the calling function. This is where pass by reference comes in handy. • Problem: Write a subprogram to swap the contents of two variables in the calling function.

  12. Specification for Swap We want to input two integer variables num1 and num2 to a function swap, swap their contents, and return these changes to the calling function. x: 5 x: 3 Swap Function y: 3 y: 5

  13. The Swapping Algorithm Your first thought (a common one) : x = y; y = x; X Y

  14. Algorithm for Swap A temporary bucket is mandatory to make this algorithm work. temp = x; x = y; y = temp; X Y Temp

  15. Coding Our Swap Algorithm • Now, we might consider calling this function by: • swap (num1, num2); • Since C passes arguments to functions by value only, there is no direct way for the called function to alter a variable in the calling function.

  16. Coding Our Swap Algorithm For instance, the following function will not work: void swap (int x, int y) /* WRONG */ { int temp; temp = x; x = y; y = temp; }

  17. Coding Our Swap Algorithm • Because of pass by value, swap can’t effect the arguments num1 and num2 in the routine that called it. • The function we just saw only swaps copies of num1 and num2.

  18. Coding Our Swap Algorithm • The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: • swap (&num1, &num2); • Since the operator & produces the address of a variable, &num1 is a pointer to num1. • In swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them.

  19. Pictorially, we want: In Our Main Program: Num2: Num1: In swap: y: Temp: x: Num2 Address Num1 Address

  20. Coding Our Swap Algorithm void swap (int *x, int *y) /* Switch *x and *y */ { int temp; temp = *x; *x = *y; *y = temp; }

  21. Coding Our Swap Algorithm • WARNING: Coding this algorithm using global variables is not an acceptable alternative to avoid passing pointers to the values to be changed. • Remember the only variables our functions should use are those in the argument list or those declared locally.

  22. Summary of Key Points • Functions can modify the variables in the calling function using the pass by reference method. • The pass by reference method involves passing pointers to the values to be changed. • The swap algorithm is a very common application of the pass by reference method. We’ll see it again in a few weeks when coding sorting algorithms.

More Related