




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!





In this chapter, we examine the difference between function calls in C and C++ and the resulting difference in the way functions are defined in the two languages. Finally, we illustrate the semantics of the three methods of passing a parameter in C++ and the three kinds of function return values. You should use the demo program and its output as reference material when you are uncertain about how to use the various parameter and return types.
Knowledge is of two kinds. We know a subject ourselves, or we know where we can find information on it. — Samuel Johnson, English, author of the Dictionary of the English Language.
The implied argument. In C++, every call to a class function has an “implied argument”, which is the object whose name is written before the function name. The rest of the arguments are written inside the parentheses that follow the function name. Additional explicit arguments are written inside parentheses, as in C. Compare this C++ code fragment to the C code fragment below:
if ( in.eof() )... stk->push(curtok);
The implied argument is called this. In contrast, in C, all arguments are written inside the parentheses. In the stack.c example, the end-of-file test and the call on push() are written like this: if (feof( in ))... push(&stk, curtok);
Number of parameters. Function calls in C++ tend to be shorter and easier to write than in C. In C, functions often have several parameters, and functions with no parameters are rare. In the stack.c example, note that the call on push() has two arguments in C and one in C++. The difference is the stack object itself, which must be passed by address in C and is the implied argument in C++. Functions with empty or very short parameter lists are common in C++. First, the implied argument itself is not writtten as part of the argument list. Also, classes are used to group together related data, and that data is available for use by class functions. We often eliminate parameters by using class data members to store information that is created by one class function and used by others.
Code organization. When writing in a procedural language, many programmers think sequentially: do this first, this second, and so on. Function definitions often reflect this pattern of thought Thus, in C, functions tend to be organized by the sequence in which things should be done. In an object-oriented language, the code is not arranged sequentially. Thus, in C++, functions are part of the class of the objects they work on. Control passes constantly into and out of classes; consecutive actions are likely to be in separate functions, in separate classes with a part-whole relationship. One object passes control to the class of one of its components, which does part of the required action and passes on the rest of the responsibillity to one of its own components.
The number and length of functions. There are many more functions in a C++ program than in a C program and C++ functions tend to be very short. Each C++ function operates on a class object. The actions it performs directly should all be related to that object and to the purpose of that class within the application. Actions related to class components are delegated (passed on) to the components to execute. Thus, long functions with nested control structures are unusual, while one-line functions and one-line loops are common. Inline expansion is used to improve the efficiency of this scheme.
Non-class functions. Functions can be defined inside or outside classes in C++. For those defined outside a class, the syntax for definitions and calls is the same as in C. Non-class functions in C++ are used primarily to modularize the main program, to extend the input and output operators, and to permit use of certain C library functions.
C supports two ways to pass a parameter to a function: call-by-value and call-by-pointer. C++ supports a third parameter-passing mechanism: call-by-reference. The purpose of this section is to demonstrate how the three parameter-passing mechanisms work and to help you understand which to use, when, and why.
These three functions do not compute anything sensible or useful; their purpose is to illustrate how the three parameter-passing mechanisms are similar and/or different. Each function computes the average of its two parameters, then prints the parameter values and the average. Finally, both parameters are incremented. The computation works the same way in all versions. However, the increment operator does not work uniformly: sometimes a pointer is incremented, sometimes an integer; sometimes the change is local, sometimes not.
Call by value. The argument values are copied into the parameter storage locations in the function’s stack frame. There is no way for the function to change values in main’s stack frame.
1 #include "odds.hpp" // Includes necessary libraries. 2 3 void odd1( int aa, int bb ){ // Make copies of the argument values. 4 int ansr = (aa + bb) / 2; // See diagram 1. 5 cout <<"\nIn odd1, average of " <<aa <<", " <<bb <<" = " <<ansr <<endl; 6 ++aa; ++bb; // Increment the two local integers. 7 }
Call by pointer. The arguments must be addresses of integer variables in the caller’s stack frame. These addresses are stored in the parameter locations in the function’s stack frame. They permit the function to modify its caller’s memory locations.
8 #include "odds.hpp" // Includes necessary libraries. 9 10 void odd2( int* aa, int* bb ){ // Call by address or pointer. 11 int ansr = (aa + bb) / 2; // See diagram 2. 12 cout << "\nIn odd2, average of " <<aa <<", " <<bb <<" = " <<ansr <<endl; 13 ++aa; // increment the local pointer 14 ++*bb; // increment main’s integer indirectly 15 }
Call by reference. The arguments must be integer variables in the caller’s stack frame. The addresses of these variables are stored in the parameter locations in the function’s stack frame, permitting the function to modify its caller’s memory locations.
16 #include "odds.hpp" // Includes necessary libraries. 17 18 void odd4( int& aa, int& bb ){ // Call by reference 19 int ansr = (aa + bb) / 2; // See diagram 3. 20 cout << "\nIn odd4, average of " <<aa <<", " <<bb <<" = " <<ansr <<endl; 21 ++aa; ++bb; // increment two integers in main. 22 }
This main program calls the three odd functions with arguments selected from the array named ar. It prints the whole array before and after each call so that you can see any changes that take place. Each call (and its results) is explained in the paragraphs that follow and stack diagrams are given to help you understand how the system works.
faa4 33
faa0 11
fa90 ??
main
odd1 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 33 fac0 44 fac4 55 fac8 66
faa4 34
faa0 12
fa90 22
main
odd1 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 33 fac0 44 fac4 55 fac8 66
Figure 5.1: Execution of odd1 after computing ansr (left) and after increments (right).
main
odd2 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 33 fac0 44 fac4 55 fac8 66
faa
faa
fa90 ??
main
odd2 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 34 fac0 44 fac4 55 fac8 66
faa
faa
fa90 22
Figure 5.2: Execution of odd2 after computing ansr (left) and after increments (right).
Call by pointer or address. Even in a call-by-value language, an argument that is an address or the value of a pointer can be used with a pointer parameter to give the function access to the underlying variable in the stack frame of the caller. The parameter can be used two ways within the function. By using the parameter name wiithout a *, the address stored locally in the parameter variable can be used or changed. By using the parameter name with a *, the value that is stored in the caller’s variable can be used or changed.
Call by value with a pointer argument. The first call on odd2 is diagrammed in Figure 5.2. The arguments are the values of two pointers. When these arguments are stored in the parameter locations, the parameters point at the same locations as the pointers in main(). Writing ++aa increments the local value of the pointer aa, which changes the array slot that the parameter points to (initially it was ar[1], after the increment it is ar[2]). Writing ++*bb changes the integer in the caller’s array slot.
Call by value with an & argument. The second call on odd2 is shown in Figure 5.3. The effect is the same as the prior call; The arguments are the addresses of two slots in caller’s array and are diagrammed as simple arrows. Writing ++aa changes the array slot that the parameter points to (initially it was ar[1], after the increment it is ar[2]). Writing ++*bb changes the integer in the caller’s array slot.
main
odd2 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 34 fac0 44 fac4 55 fac8 66
faa
faa
fa90 ??
main
odd2 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 34 fac0 44 fac4 56 fac8 66
faa
faa
fa90 38
Figure 5.3: Execution of odd2 after computing ansr (left) and after increments (right).
Call by reference (&). This form of parameter passing is normally used when the argument is or can be an object that is partly created by dynamic storage allocation.In call-by-reference (Figure 5.4), the parameter
value is called a “binding” and is diagrammed as a shaded double arrow.
fa90 ??
main
odd4 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 34 fac0 44 fac4 56 fac8 66
faa
faa
main
odd4 int aa
int ansr
int bb int * iq int * ip int[6] ar
fab
faac
fab4 11 fab8 22 fabc 35 fac0 44 fac4 57 fac8 66
faa
faa
fa90 45
Figure 5.4: Execution of odd4 after computing ansr (left) and after increments (right).
The value stored locally in the parameter cannot be changed because the parameter name becomes trans- parent: it is just an alias, or a synonym, for the caller’s variable. The program cannot read or change the address stored in the parameter. For example, line 29, using ++ changes the caller’s value, not the address stored locally in the parameter.
Call by reference vs. value. When call by value is used, the function cannot change values in the caller’s stack frame. Any reference to the parameter name is a reference to the local copy, and any assignment to the parameter changes only the local copy. In contrast, when call by reference is used, the parameter name is an alias for the argument variable. Even though this is implemented by storing the address of the caller’s variable in the function’s stack frame, it does not function like a pointer. The dereference operator is not used because the dereference action is automatic. Any reference to or assignment to the parameter name IS a reference or assignment to the variable in the caller’s stack frame.
Call by reference vs. pointer. When call by pointer is used, the function can dereference the parameter and assign a new value to the argument variable in caller’s stack frame. Or, the function can change the address stored in the parameter and make it refer to a different variable. In contrast, when call by reference is used, there is no choice; because the parameter name is an alias for the argument variable. It is not possible to change the address in the local stack frame. Also, syntactically, the parameter is not a pointer and cannot be dereferenced.
An l-value is a value that can be used on the left side of an assignment operator; it is the location into which something will be stored. An r-value is a value that can only be used on the right side of an assignment operator; it is the value that will be stored. In the example below, the function ret0() returns an integer r-value, which we sometimes refer to as a “pure value”. A reference (including one that was returned by a function) can be used directly as an l-value. When used as an r-value (on the right side of =) a reference is treated the same way that a variable name would be treated in that context: the value at that address is fetched from memory. In the example below, ret3() and ret4() return l-values. A pointer is an r-value that can be dereferenced. If a function returns a pointer, the function’s result must be dereferenced before it can be used on the left side of =. In the example below, ret1() and ret2() return pointer r-values that can be dereferenced to get l-values. A dereferenced pointer can also be used as an r-value; when that is done, it will be automatically dereferenced again to get the underlying r-value.
14 #include
20 <<" " #p " --> " << dec << p 21 //----------------------------------------------------------------------- 22 void print5( int ap ) { // Print the five values in an array. 23 for( int k=0; k<5; ++k ) cout << " [" << k << "] = " << ap[k] ; 24 } 25 26 //----------------------------------------------------------------------- 27 int main( void ) 28 { 29 int ar[5] = {11, 22, 33, 44, 55}; 30 int h, j, k, m, n; 31 int *p = ar; 32 33 cout << "Initially, variables are: ------------------------------\n"; 34 print5( ar ); 35 cout <<DUMPp(p); 36 37 h = ret0( ar ); // Answer should be 11 38 j = *ret1( ar ); // Answer should be 22 39 k = *ret2( ar ); // Answer should be 33 40 m = ret3( ar ); // Answer should be 44 41 n = ret4( ar ); // Answer should be 55 42 cout <<DUMPv(h) <<DUMPv(j) <<DUMPv(k) <<DUMPv(m) <<DUMPv(n) <<endl; 43 44 p = ret1( ar ); // Answer should be a pointer to ar[1]. 45 ret1( ar ) = 17; // Storing through the pointer. 46 //ret2( ar ) = 17; // Illegal: read-only location. 47 ret3( ar ) = -2; // Assigning to the reference. 48 //ret4( ar ) = -2; // Illegal: read-only location. 49 50 cout << "\nAfter return from functions variables are: -----------\n"; 51 print5( ar ); 52 cout <<DUMPp(p) <<endl; 53 }
The Dump macros. A macro lets us define a shorthand notation for any string of characters. We use macros when the same thing must be written several times, and that thing is long and error-prone to write. In this program, we want to be able to print out information that will be useful for understanding the program. We define one macro, DUMPv that will print the address and contents of its parameter, and a second, DUMPp, that prints the address of a pointer variable, the address stored in it, and the value of its referent. A macro differs from an inline function in several ways. A very important difference is that macro parameters are not typed, and macro calls are not type checked. Thus, the macro defined here can be used to dump any object for which << is defined.
The main function. We define an array, initialize it to easily-recognized values, and print it before and after calling the ret functions. By comparing the lines at the beginning and end of the output, you can verify that lines 45 and 46 changed the contents of the array. Calls on the DUMP macros in lines 35, 42, and 50 allow you to trace the effects of each function call.
The output is:
Initially, variables are: ------------------------------ [0] = 11 [1] = 22 [2] = 33 [3] = 44 [4] = 55 p @ 0xbffffd40 value = 0xbffffd18 p --> 11
h @ 0xbffffd2c value = 11 j @ 0xbffffd30 value = 22 k @ 0xbffffd34 value = 33 m @ 0xbffffd38 value = 44 n @ 0xbffffd3c value = 55 After return from functions variables are: ----------- [0] = 11 [1] = 17 [2] = 33 [3] = -2 [4] = 55 p @ 0xbffffd40 value = 0xbffffd1c p --> 17
Return by value. This is the commonest and safest kind of function return; the returned value is an r-value. (An r-value can be used on the right side of an assignment but not on the left.) Among the functions above, ret0() returns an integer by value. The sample program calls ret0() (line 37) and assigns the result to an integer variable.
Return by pointer. The return value of ret1() is a non-const pointer r-value (the address of ar[1]) that can be stored in a pointer variable or dereferenced to get an l-value. It is called three times:
j = *ret1( ar ); // Copy the referent of the return value.
p = ret1( ar ); // Store the return value in a pointer variable.
*ret1( ar ) = 17; // Store through the pointer into its referent.
Return by const*. The return value is an address that provides read-only access to the underlying variable. The function result cannot be used on the left side of an assignment to change the underlying variable. In the sample program, ret2() is called once (line 39). Its result is dereferenced to get an integer l-value, which is automatically dereferenced again because it is on the right side of an assignment statement. The result is an int r-value, which is stored in k:
k = *ret2( ar ); // Copy referent of return value into k.
Return by reference. A reference can be used in the same ways that a variable name is used. The function ret3() returns an integer reference: an l-value that can be used (without *) on either side of an assignment operator. This function is called twice and used both as an r-value (line 40) and as an l-value (line 46):
m = ret3( ar ); // Coerce function result to r-value and store. ret3( ar ) = -2; // Store -2 in address returned by function.
Return by const reference. The function ret4() returns a const int& (a reference to a const int). This result can be only be used for read-access, because the underlying variable is a constant. ret4() is called once (line 41) and used with implicit dereference on the right side of an assignment statement:
n = ret4( ar ); // Copy referent of return value into n.
Illegal calls. The following lines won’t compile. For both, my compiler gives the error comment assignment of read-only location.
*ret2( ar ) = -5; // return by const * ret4( ar ) = 25; // return by const &