Call by Reference

In traditional C, a function call passes arguments by value. The called function create a new set of variable and copies the value of argument into them. The function does not have access to the actual variable in the calling program and can only work on the copies of values. This machanism 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 value of the variables in the calling program. For example, in bubble sort, we compare two adjacent element in the list and interchange their values if the first element is greater than the second. If a function is used for bubble sort, then it should be able to alter the value of the variable in the calling function, which is not possible if the call-by-value method is used.

No comments:

Post a Comment