Saturday, May 29, 2010

GOOD EXAMPLE OF CALL BY REFERENCE VS CALL BY VALUE

How many times you get in a situation and think what to use in your function arguments- Call By Reference Or Call By Value?

This ain't tricky et al if you remember all basic simple thing i.e. In call by value, a function call passes arguments by value. The called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of values. This mechanism is fine if the function does not need to alter the values of the original variables in the calling program. But, there may arise situation where we would like to change the values of variables in the calling program. One good example is Bubble Sort, we compare the two adjacent elements in the list and interchange their values if the first element is greater than the second. Now if the function is used for bubble sort, then it should be able to alter the values of variables in the calling function as well, which is not possible if the call by value method is used.
I'm sure this will help you remember the difference for your life now :)

No comments:

Post a Comment