CPSC 259 Chapter 3.1: Pass by reference and value

58 views2 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents