Briefly, call by reference is when a function can modify its arguments:
def f(x):
x := x + 1
print(x)
x := 1
f(x)
/* Now, x is 2 */
print(x)
In "call by reference", the above will print 2
twice.
Call by value is when a function receives a copy of the argument passed to it, so any modifications don't get seen by the caller. With f(x)
defined as above, call by value would be:
x := 1
f(x)
/* x is 1 in the caller */
print(x)
In the above, inside f(x)
, the print call would print 2
, but that only modifies the copy of x
that f()
got, so in the caller, x
is still one, and the second print()
prints 1
.