8.6. Call by value

When you pass a structure as an argument, remember that the argument and the parameter are not the same variable. Instead, there are two variables (one in the caller and one in the callee) that have the same value, at least initially. For example, when we call printPoint, the stack diagram looks like this:

image

If printPoint happened to change one of the instance variables of p, it would have no effect on blank. Of course, there is no reason for printPoint to modify its parameter, so this isolation between the two functions is appropriate.

This kind of parameter-passing is called “pass by value” because it is the value of the structure (or other type) that gets passed to the function.

Take a look at the active code below. Notice from the output of the code below how the function addTwo changes the instance variables, but not on blank itself.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 4 activities on this page