This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ent is copied to this slot
x Consequently, modifying the arguments inside the function
Consequently, does not change the value of the variables used in the call
x Pass-by-value is the default mechanism in C++ for scalar
Pass-by-value types (int, float, double, bool, char, short, and their
x A lot of memory can be consumed: think about passing-by- value a HD movie, for a function that returns its name!
x C++ allows at most one item to be returned. How to model
C++ algorithms with multiple outputs?
x Data movement is costly
x Sometimes, the data is not fully known in advance 87 Pass-by-reference
x Central idea: instead of copying the value of the argument,
Central simply copy the address where the arguments are stored
x Major differences with pass-by-value Modifying the value of the arguments inside the function changes
their value at the call site
their Whatever the type of data, only 32 bits (or 64 bits)
x In C++, append the & symbol to the type of the argument to
In mark an argument as passed-by-reference
x Be careful: some types are pass-by-reference by default
Be (i.e., arrays)
(i.e., 88 More on references
x A reference to a variable can be declared within a function It is a standard type qualifier
x Be careful: when a function terminates, the (temporary)
Be memory slots allocated to it are “destroyed”
x A reference can be passed as argument, an...
View Full Document
- Fall '00