An Example of Pass-By-Value
Figure 8.6 and Fig. 8.7 present two versions of a function that cubes an integer. Figure 8.6 passes variable
number by value (line 14) to function
cubeByValue (lines 19–22), which cubes its argument and passes the new value back to
main using a
return statement (line 21). The new value is assigned to
number (line 14) in
main. The calling function has the opportunity to examine the function call’s result before modifying variable
number’s value. For example, we could have stored the result of
cubeByValue in another variable, examined its value and assigned the result to
number only after determining that the returned value was reasonable.
1 // Fig. 8.6: fig08_06.cpp 2 // Pass-by-value ...