





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Pass by Value When an argument is passed to a function using the pass by value method, essentially a copy of the argument’s value is passed to the function. It is impossible for the function to modify the actual argument since it does not “know” the location of where this argument is stored in the memory, it only has a copy of the value that is stored in that location. copied Memory Figure 1. Configuration at the time Function X calls Function Y. Copy of actual parameter a is copied into location of formal parameter m. Figure 2. Configuration while Function Y is in execution. Assume Y changes the value of m. Figure 3. Configuration just after Function Y has completed execution.
Calling function: X actual parameter: a Called function: Y formal parameter: m memory cell of a 27 memory cell of m 27 Calling function: X actual parameter: a Called function: Y formal parameter: m memory cell of a 27 Calling function: X actual parameter: a Called function: Y formal parameter: m memory cell of a 27 memory cell of m 19
Pass by Value An illustration of how the actual parameter in the calling function is unchanged by the actions of the called function. double click in this window to start the show Figure 4. PowerPoint slide shown illustrating pass-by-value. Parameters in the C language are passed by value. This restriction of the C language is in many ways an asset rather than a liability. It usually leads to more compact code that contains fewer extraneous variables, because the function arguments can be treated as conveniently initialized local variables in the called function. Pass by Reference Sometimes it is necessary to arrange for a called function to modify a variable in the calling function. This requirement means that the argument must be passed by reference. In order to achieve the effect of call-by-reference in C, we must use pointers in the formal parameter list in the function definition and pass addresses of variables as actual parameters in the function call. [As we will see later, this is the only way in which an array can be passed in C, i.e., it is not possible to pass an array by value in C.] You are already familiar with this technique as this is what we have been doing in the scanf( ) statements that we have been using in our programs to date. The function call scanf(“%d”, &alpha); causes an appropriate value to be stored at the address in memory which is identified by the identifier alpha. Recall that &alpha is the address, or location in memory of the variable alpha. Pass by Reference An illustration of how the actual parameter in the calling function can be changed by the actions of the called function. double click in this window to start the show Figure 5. PowerPoint slide show illustrating pass-by-reference.
We can not use the pointer p to access the value stored in the location referred to by a. This is done through the dereference or indirection operator . The indirection operator is a unary operator with the same precedence as all other unary operators and it associates from right-to-left. Since p is a pointer, the expression * p has the value of the variable to which p points. printf(“p = %d\n”, *p); Since p points to the location identified by a , and a contains the value 7, the dereferenced value of p is also 7, and this is the value that will be printed by the printf statement shown above. No consider the following: *p = 3; printf(“a = %d\n”, a); What value do you expect will be printed by the printf statement? It will print 3. The reason is this: The first statement assigns to the location referenced by p the value 3. Since p refers to the location that is identified by a , the value of 7 stored at a is overwritten with the value 3. Thus, when we print the value of a it has been changed through the reference to that location by p. p = &b; //changes p to refer to same location as b a b p *p = 2 * *p – a; //change value in location referenced by b printf(“b = %d\n”, b); a b p
Using Pointers and Addresses in Functions Write a function to swap the values of two integers. Why doesn’t this function work? What is the output of this program? The answer is that the variables in the function (including the parameters) are local to the function, they do not exist after the function executes, so a and b are exactly the same before and after the function. What we need is the function to have access to the actual locations of a and b and not just their values! To do this we must pass the arguments by reference not by value. To do this we must declare the formal parameters of the function to be pointers. Inside the function we will use the dereferenced pointer to gain access to the actual parameter locations and hence their values. So we will be passing not copies of the arguments but rather the address of the argument. //function to swap two integer values // // INCORRECT VERSION void swap (int x, int y) { int temp; tmp = x; x = y; y = temp; } int main (void) { int a = 3; b = 7; printf (“%d %d\n”, a, b); swap(a, b); printf (“%d %d\n”, a, b); return 0; }
Often you will need a function to return more than one value to the caller. This is easily accomplished with parameters that are passed by reference. The following example illustrates a function that prompts the user for two integer values to be input. Calls to the function getData would be of the form: getData (&thelength, &thewidth); Calls to the function getData in the following form are illegal: getData (8, 7); or getData(thislength, thiswidth+3); Tracing Code with Pointers //function: getData // prompts the user for two input integers representing the length //and width of a rectangle //Outputs: two integers corresponding to width and length. void getData ( int *length, int *width) { printf (“Enter the length:”); scanf (“%d”, length); printf (“Enter the width:”); scanf (“%d”, width); }
Execute the following code by hand (i.e., trace the code) and show exactly what is produced by each printf statement in the main program as well as in the functions when they execute. Before looking at the next page, try to trace through this code and see if you get the same output as shown on the next page. Modularity: Functions - (^22) //mystery program….you tell me what it does // #include <stdio.h> int f1 (int *a, int b); inf f2 (int a, int *b); int main ( ) { int a = 5, b = 2, c = 7, d = 9; c = f1 (&d, a); printf (“Main 1: a = %d, b=%d, c=%d, d=%d\n”, a, b, c, d); a = f2 (c-d, &a); printf (“Main 2: a = %d, b=%d, c=%d, d=%d\n”, a, b, c, d); b = f1 (&c, 8); printf (“Main 3: a = %d, b=%d, c=%d, d=%d\n”, a, b, c, d); d = f2 (b, &a); printf (“Main 4: a = %d, b=%d, c=%d, d=%d\n”, a, b, c, d); } int f1 (int a, int b) { a = b – 8; b = b2 – (a); printf (“In f1: a = %d, b = %d\n”, *a, b); return b - *a; } int f2 (int a, int *b) { a = *b + 1; *b = 37 - *b; printf (“In f2: a = %d, b = %d\n”, a, *b); return a; } In f1: a = -3, b = 13 Main 1: a = 5, b = 2, c = 16, d = - In f2: a = 6, b = 32 Main 2: a = 6, b = 2, c = 16, d = - In f1: a = 0, b = 16 Main 3: a = 6, b = 16, c = 0, d = - In f2: a = 7, b = 31 Main 4: a = 31, b = 16, c = 0, d = 7