Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

pass the variable, please

One property that has tremendous bearing on any programming language is how variables are passed. There are generally two methods, passing by reference and passing by value.

Passing by reference takes the memory location of the variable or pointer and passes that into the function that takes the parameter. In short, this means that any work or changes done to the object or primitive is changed for good.

The other method is passing by value. This copies the reference to the object or variable and passes it in to the function. This allows for manipulation with the data from the variable or object without changing the actual contents of the data.

Both of these methods are valuable, and in my opinion the languages that can do both (like C#) are especially useful. In fact, this is one of the differences between Java and C#, Java only passes by value, C# allows passing by reference (through the ref keyword.) Without being able to pass by reference, Java becomes a more strongly-typed langauge, you have to go further to manipulate actual data. However, this also makes a simple program like swapping two string variables inordinately complicated compared to languages that pass by reference.

Knowing which technique the language you are using implements is of primary importance, failure to recognize whether a language passes by reference or by value will cause hours of frustration in seemingly inexplicable errors and unexpected return values.

This post first appeared on Coding Flies, please read the originial post: here

Share the post

pass the variable, please


Subscribe to Coding Flies

Get updates delivered right to your inbox!

Thank you for your subscription