Passing by reference means that, when you pass a variable into the function, you don't pass the variable itself, you pass the pointer to the variable, which is copied from outside to inside the function. In Example 1, you pass list
into the function, which is a pointer to a list that contains the elements [3]
. But then, immediately after, you take that variable holding the pointer to the list, and put a new pointer in it, to a new list that contains the elements [0, 1, 2]
. Note that you haven't changed the list you started with - you changed what the variable referring to it referred to. And when you get back out of the function, the variable you passed into the function (still a pointer to the first list) hasn't changed - it's still pointing to a list that contains the elements [3]
.
In Example 2, you pass A
into xyz()
. Whereas in Example 1 you did something along the lines of
A = something_else
here, you're doing
A[i] = something_else
This is an entirely different operation - instead of changing what the variable holding the list is pointing to, you're changing the list itself - by changing one of its elements. Instead of making A
point to something else, you're changing the value that A
points to by dereferencing it.