Question: Before learning how to pass a variable by reference, we will review the concept of passing by value, which you learned about in Chapter 9

Before learning how to pass a variable by reference, we will review the concept of passing by
value, which you learned about in Chapter 9.
Reviewing Passing Variables by Value
Recall that unless you specify otherwise, variables are passed by value in C++. This means that
the computer passes only a copy of the variables contents to the receiving function. When only
a copy of the contents is passed, the receiving function is not given access to the variable in
memory, and, therefore, it cannot change the value stored inside of the variable. It is appropriate
to pass a variable by value when the receiving function needs to know the variables contents but
does not need to change the contents.
The company ratings program shown in Figure 10-8 passes a variable by value to a void
function named displayRating. The function definition is located below the main function
(on Lines 28 through 34). Therefore, the program includes an appropriate function prototype
above the main function (on Line 8). Because the displayRating function is a void function,
its function call (on Line 18) appears as a statement by itself. The function call passes the
numStars variable by value to the void function. This means that only a copy of the variables
value is passed to the function, which stores that value in its formal parameter (num). The
displayRating function does not have access to the numStars variable. It is not even aware
of the variables existence in the computers internal memory.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!