Andy D. Digh Friday, May 29th, 1998

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

description

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. - PowerPoint PPT Presentation

Transcript of Andy D. Digh Friday, May 29th, 1998

Page 1: Andy D. Digh Friday, May 29th, 1998

Passing Function Passing Function Arguments by Arguments by

Reference :Reference :A Sample Lesson for CS 1 A Sample Lesson for CS 1

using theusing the C Programming Language C Programming Language

Andy D. DighAndy D. Digh

Friday, May 29th, 1998Friday, May 29th, 1998

Page 2: Andy D. Digh Friday, May 29th, 1998

So far, all the function calls we’ve So far, all the function calls we’ve seen have passed one or more seen have passed one or more arguments into a function using a arguments into a function using a method we call method we call pass by valuepass by value..

Consider our call to function Consider our call to function find_max:find_max:

Passing Function Arguments by Passing Function Arguments by ValueValue

max = find_max (num1, num2);

Page 3: Andy D. Digh Friday, May 29th, 1998

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;}

Page 4: Andy D. Digh Friday, May 29th, 1998

Pictorially here showing the pass by value we have:

In Our Main Program:

Num1: Num2:7 4

Page 5: Andy D. Digh Friday, May 29th, 1998

Now, once function find_max is called we have:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

?

Page 6: Andy D. Digh Friday, May 29th, 1998

Now, the maximum is determined in our subprogram and stored in Temp:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

7

Page 7: Andy D. Digh Friday, May 29th, 1998

Now, the maximum is determined in our subprogram and stored in Temp:

In Our Main Program:

Num2:7Num1: 4

In find_max:

7x: 4y: Temp:

Max: ?

7

Page 8: Andy D. Digh Friday, May 29th, 1998

Once our function returns our result, the function data area is always erased and will be recreated when the procedure is called again.

In Our Main Program:

Num2:7Num1: 4 Max: 7

Page 9: Andy D. Digh Friday, May 29th, 1998

The functions we’ve seen with pass The functions we’ve seen with pass by value have also always returned a by value have also always returned a singlesingle value. value.

This has corresponded nicely to our This has corresponded nicely to our understanding of functions from understanding of functions from mathematics.mathematics.

Passing Function Arguments by Passing Function Arguments by ValueValue

F(x) = x2 + 3

F(2) = 22 + 3 = 7

Page 10: Andy D. Digh Friday, May 29th, 1998

They’ve also never modified a They’ve also never modified a variable in the calling function.variable in the calling function.

But, sometimes we aren’t always But, sometimes we aren’t always interested in cranking out one result interested in cranking out one result in our subprograms. That is, we in our subprograms. That is, we want our subprograms to produce want our subprograms to produce multiple values.multiple values.

Passing Function Arguments by Passing Function Arguments by ValueValue

Page 11: Andy D. Digh Friday, May 29th, 1998

In effect, we want to be able to alter In effect, we want to be able to alter a variable in the calling function. This a variable in the calling function. This is where is where pass by referencepass by reference comes in comes in handy. handy.

ProblemProblem: Write a subprogram to : Write a subprogram to swap the contents of two variables in swap the contents of two variables in the calling function.the calling function.

Passing Function Arguments by Passing Function Arguments by ReferenceReference

Page 12: Andy D. Digh Friday, May 29th, 1998

We want to input two integer variables We want to input two integer variables num1num1 and and num2num2 to a function to a function swap,swap, swap their contents, and return swap their contents, and return these changes to the calling these changes to the calling function.function.

Specification for SwapSpecification for Swap

y:

x: 5

3 y:

x: 3

5

Swap

Function

Page 13: Andy D. Digh Friday, May 29th, 1998

Your first thought(a common one) :

x = y;

y = x;

X

The Swapping AlgorithmThe Swapping Algorithm

Y

Page 14: Andy D. Digh Friday, May 29th, 1998

Algorithm for SwapAlgorithm for Swap

A temporary bucket is mandatory to make this algorithm work.

temp = x;

x = y;

y = temp;

X

Temp

Y

Page 15: Andy D. Digh Friday, May 29th, 1998

•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.

Coding Our Swap AlgorithmCoding Our Swap Algorithm

Page 16: Andy D. Digh Friday, May 29th, 1998

Coding Our Swap AlgorithmCoding 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;

}

Page 17: Andy D. Digh Friday, May 29th, 1998

•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.

Coding Our Swap AlgorithmCoding Our Swap Algorithm

Page 18: Andy D. Digh Friday, May 29th, 1998

Coding Our Swap AlgorithmCoding 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.

Page 19: Andy D. Digh Friday, May 29th, 1998

Pictorially, we want:

In Our Main Program:

Num2:Num1:

In swap:

x: y: Temp:Num1 Address

Num2 Address

Page 20: Andy D. Digh Friday, May 29th, 1998

Coding Our Swap AlgorithmCoding Our Swap Algorithm

void swap (int *x, int *y) /* Switch *x and *y */

{

int temp;

temp = *x; *x = *y; *y = temp;

}

Page 21: Andy D. Digh Friday, May 29th, 1998

Coding Our Swap AlgorithmCoding 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.

Page 22: Andy D. Digh Friday, May 29th, 1998

Summary of Key PointsSummary 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.