CSE100Lab08ManskiJosh - Lab Section Name Lab 8 Functions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Section:______ Name:______________________________________ Lab 8 Functions, Part III Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. This lab will cover: passing parameters by reference variable scope Passing by Reference: Up until now, the functions we've been writing in lab have all been using a technique called passing by value . Basically, this means that when we pass a variable to a function as a parameter, the value of that variable is copied and inserted into the function, but the original variable remains unchanged. For example, if we ran this code: void increment(int y) { y++; return; } int main() { int x = 4; increment(x); cout << x << endl; } We would get the value 4 output to the console. This is because when we called increment(x) , we actually gave the local variable y the value of x (4), and left x alone. So y got incremented to 5, but when we returned from the function, that value was lost to the netherworld. If we wanted to change increment so that it actually changed the value of the variable being passed in to it, this is how we would re-write the function: { y++; return; } Take note of that little ampersand (&) next to the variable y , that's what will cause the behavior of the function to change. Now, if we used the same main function above and ran the code using this new function, the value 5 would be output to the console. This is an example of passing by reference . When code passes parameters by reference, it actually sends the variable over to the function for use (instead of just a copy of its value). So, when we called increment(x) with the new version of increment, it actually sent x itself over to the function, renaming it to y temporarily while it was inside the function (since that was the name we gave the parameter in the function header). So, when we incremented the value of y , we were actually incrementing x , which is why in the new version of the function x got changed to 5 and stayed that way after the function ended. CSE 100 Lab 8 1/6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
So remember, to pass parameters by reference, put an ampersand (&) right before the name of the parameter(s) in your function header. You don't have to change anything on the function calling side (variables still get passed like normal, functions still get called like normal), just remember that the variables you pass to that function can be changed by that function from then on. One disadvantage of passing by reference is that you can't pass raw values as parameters. For instance, in the new version of increment above, you would generate an error if you tried to put the line "
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

CSE100Lab08ManskiJosh - Lab Section Name Lab 8 Functions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online