By Value or By Reference

The second method of transferring information to a function is to pass it By Reference. This means that a pointer or reference to where the data is stored in memory is passed to the function and not a copy of the data. Since a pointer to where the data exists has been passed, if you actually change the value of the data in the function, the actual values of the data in the main program where the function was called from will also be changed. This can be very powerful but also very dangerous. Be careful passing parameters By Reference, you might mistakenly change a value when that is not what you or someone else is expecting. The rule of thumb is that unless there is a really good reason to pass something By Reference, you never do and you always pass parameters By Value (even though it takes up more space in memory).

Each language has its own syntax on how to declare you are going to accept a value by reference when you are declaring a function here is an example:

Code for Function passing a value By Reference

Example Output

Code example output