Pass by Value
In this lesson, you’ll look at a mechanism for argument passing called pass by value. Here’s a C++ version of that same
square() function from the last lesson.
C++ requires a lot of type information to indicate the kind of values the variables will represent. In this case, I’d like decimal numbers and double precision, so you see the word
But you can also see the parameter
num still exists. It’s still being multiplied by itself to perform the square operation, and then its value is returned.
Here, again, you can see that same function after some standard lines of code that almost all C++ programs need to work. Take your parameter
num, square it, and return it.
Since I don’t have a way to run C++ code interactively, I’ll have to write something like a script to test it. In C++, we call that a main function. You can see inside this main function that I create a variable
I’m saying it’s an integer, but C++ can make it a decimal value when the function is called. The next line is C++’s unique output statement. It begins with the word
cout. Then, with less than sign type of arrows (
<<), we indicate what we want printed.
In this case, we want the return value of the function
square() being called on the value in
4. The last
endl is C++’s way to indicate the end of the line.
01:42 I’ll get in my terminal shell to run this. C++ programs need to be compiled, and this next statement does that.
And then to run it, I type
square with some extra punctuation to make it execute.
And there you see the result of
16, just as we expected.
C++ uses something called pass by value for simple types. I can demonstrate that with a very simplified model of memory. When the main function is run, a specific place in memory is designated to hold the value of the variable
val, which has a value of
4. When the function is called, a new place of memory is made to hold the value of
num, and then the value of
4 is copied from
val to that memory location.
That’s what we mean by “pass by value.” The value is copied from the argument to the parameter. When the function finishes running, the memory for
num is removed but the memory location for
val remains until the program itself quits running.
Now, I know this is a very simplified explanation of how memory works, but hopefully it illustrates the point that I want to make. The value of
4, is copied to be the value of
num. “Pass by value.” Pass by value works for simple types, but not for more complicated structures.
03:19 Here is a simple C++ function to find the mean of a collection of numbers—in this case, provided in something called an array. In C++, we have to pass the array and the size of the array as arguments to this function.
The array is being saved in the parameter called
a, and the size in
I won’t go into the details of how this function works. Just know that it loops through the collection, accumulating the sum as it goes. Then it divides that by the size of the collection. Below it, I have a main function that creates an array called
numbers and then passes that array and its size to the
04:09 The result is then printed, like our previous program. I can go back to the terminal, compile, and run it.
6.5 is indeed the mean of those ten values.
Let’s consider what memory would look like if this collection was being passed by value. First, memory would be created for the array
Then, when the function was called, the entire array would be copied into a new location for the parameter
a, the point being that the entire array would be duplicated in memory.
That’s not necessarily a big deal if the array size is
10, but what if it were something larger, or much larger? That would be a lot of repeated information in memory.
05:12 And even if memory size isn’t a problem, there’d be a lot of computation time to copy the entire array from one location in memory to another. So, pass by value might not be the best mechanism for more complicated types. This is where pass by reference comes in.
05:31 By the way, Python doesn’t use pass by value. You’re just seeing it now to compare it to pass by reference, which you will then compare to Python’s mechanism.
Become a Member to join the conversation.