cs31 lecture 17

E arrays ie 88 more on references x a reference to a

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ent is copied to this slot argument x Consequently, modifying the arguments inside the function Consequently, does not change the value of the variables used in the call does x Pass-by-value is the default mechanism in C++ for scalar Pass-by-value types (int, float, double, bool, char, short, and their variations)
 variations)
 86 Limitations 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? algorithms 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 simply x Major differences with pass-by-value Modifying the value of the arguments inside the function changes Modifying their value at the call site their Whatever the type of data, only 32 bits (or 64 bits)
 are copied x In C++, append the & symbol to the type of the argument to In mark an argument as passed-by-reference mark 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” memory x A reference can be passed as argument, an...
View Full Document

Ask a homework question - tutors are online