CPSC 259 Chapter 3.1: Pass by reference and value
Document Summary
Pass by reference, pass by value and pointers. Pointers can behave in strange ways when used in functions, that needs to be accounted for. So first we discuss pass by reference and pass by value and then the relations of pointers to these concepts. Pass by value happens when a function gets the copy of the value of a variable as an input. Once the function has been executed, all the variables disappear. Int a, b = 5; int c; c = add (a,b); The value of the variables e, f and d is deallocated as soon as the return line is executed. Fortunately, we have returned that value to c, and hence the deallocation does not affect our code. Now pass by reference, happens when you pass the address of the variable, instead of its value. So in this case, had we passed &c, instead of passing c itself than that would be passing by reference.