< Previous | Contents | Next >

Passing References to Alter Arguments

Now that youve seen how references work, you might be wondering why youd ever use them. Well, references come in quite handy when you are passing variables to functions because when you pass a variable to a function, the function gets a copy of the variable. This means that the original variable you passed (called the argument variable) cant be changed. Sometimes this might be exactly what you want because it keeps the argument variable safe and unalterable. But other times you might want to change an argument variable from inside the function to which it was passed. You can accomplish this by using references.


 

Introducing the Swap ProgramPassing by ValuePassing by Reference