I would like to clarify the differences between by value and by reference.
I drew a picture
So, for passing by value,
a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy
How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference "
Thanks!
Thanks so much everyone for all these input!
I quoted that sentence from a lecture note online: http://www.cs.cornell.edu/courses/cs213/2002fa/lectures/Lecture02/Lecture02.pdf
the first page the 6th slide
" Pass by VALUE The value of a variable is passed along to the function If the function modifies that value, the modifications stay within the scope of that function.
Pass by REFERENCE A reference to the variable is passed along to the function If the function modifies that value, the modifications appear also within the scope of the calling function.
"
Thanks so much again!
I'm not sure if I understand your question correctly. It is a bit unclear. However, what might be confusing you is the following:
When passing by reference, a reference to the same object is passed to the function being called. Any changes to the object will be reflected in the original object and hence the caller will see it.
When passing by value, the copy constructor will be called. The default copy constructor will only do a shallow copy, hence, if the called function modifies an integer in the object, this will not be seen by the calling function, but if the function changes a data structure pointed to by a pointer within the object, then this will be seen by the caller due to the shallow copy.
I might have mis-understood your question, but I thought I would give it a stab anyway.
When passing by value:
and then calling
you will construct an
Object
on the stack, and within the implementation offunc
it will be referenced byo
. This might still be a shallow copy (the internals ofa
ando
might point to the same data), soa
might be changed. However ifo
is a deep copy ofa
, thena
will not change.When passing by reference:
and then calling
you will only be giving a new way to reference
a
. "a
" and "o
" are two names for the same object. Changingo
insidefunc2
will make those changes visible to the caller, who knows the object by the name "a
".As I parse it, those words are wrong. It should read "If the function modifies that value, the modifications appear also within the scope of the calling function when passing by reference, but not when passing by value."
I think much confusion is generated by not communicating what is meant by passed by reference. When some people say pass by reference they usually mean not the argument itself, but rather the object being referenced. Some other say that pass by reference means that the object can't be changed in the callee. Example:
Some people would claim that 1 and 3 are pass by reference, while 2 would be pass by value. Another group of people say all but the last is pass by reference, because the object itself is not copied.
I would like to draw a definition of that here what i claim to be pass by reference. A general overview over it can be found here: Difference between pass by reference and pass by value. The first and last are pass by value, and the middle two are pass by reference:
I vote for the following definition:
That means that the following is pass by value:
1
is pass by value, because it's not directly bound. The implementation may copy the temporary and then bind that temporary to the reference.2
is pass by value, because the implementation initializes a temporary of the literal and then binds to the reference.3
is pass by value, because the parameter has not reference type.4
is pass by value for the same reason.5
is pass by value because the parameter has not got reference type. The following cases are pass by reference (by the rules of 8.5.3/4 and others):My understanding of the words "If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference" is that they are an error.
Modifications made in a called function are not in scope of the calling function when passing by value.
Either you have mistyped the quoted words or they have been extracted out of whatever context made what appears to be wrong, right.
Could you please ensure you have correctly quoted your source and if there are no errors there give more of the text surrounding that statement in the source material.