Home     Blog

Call-By-Reference in Functions

The call by reference uses a different mechanism. In place of passing value to the function, which is called, a reference to the original variable is passed. A reference is an alias for the predefined variable. That is, the value of that variable can be accessed by using any of the two: the original or the reference variable name.

When a function is called by reference, then the formal parameters become reference to the actual parameters in the calling function. This means that, in call by reference method, the called function does not create its own copy of the original values, rather, it refers to the original values only by different names. This called function works with the original data and any change in the value is reflected back to the data.

To write a function, which returns multiple output, we have to pass parameters by reference.

function pass by reference clip image002 Call By Reference in Functions

We use & to denote a parameter that is passed by reference:

Example:

  • The address (reference) of the actual variable is passed to the function, instead of its value.
  • If the function changes the parameter's value, the change is reflected back in the actual variables in the called function, since they share the same memory location.

function pass by reference clip image004 Call By Reference in Functions

function pass by reference clip image006 Call By Reference in Functions

In the above program, the function change() refers to the original value of orig, which is 10, by its reference a. The same memory location is referred to by orig in main() and by a in change(). Hence the change in a , by assigning value20, is reflected in orig also.

function pass by reference clip image008 Call By Reference in Functions

Pass-by-value Vs Pass-by-reference

function pass by reference clip image010 Call By Reference in Functions

This program uses two functions, one using pass-by-value method and the other using pass-by-reference method. This shows how the value of number does not change in the main function, when it is evaluated using pass-by-value method and on the other hand it changes when implemented using pass-by-reference method.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Functions

    A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program. A programmer can solve a simple problem in C++ with a single function. Difficult and complex problems can be decomposed into sub-problems, each of which can be either coded directly or further [...]...


  • Standard Library Functions

    A library is a collection of programs and functions which can be used by other programs. Like all other high-level languages, C++ provides function libraries containing built-In functions. These standard functions are extremely useful to the programmers. The advantages of library functions are listed below: Reusability of code : The program development activity requires three [...]...


  • Virtual Functions

    Dynamic polymorphism allows programmers to write code using functions applied to objects of the base class without worrying about how those functions will actually be defined by derived classes. For instance, programmers might call functions on a Shape object that computes its area or perimeter, without concerning themselves with the actual (derived) type of the [...]...


  • Address Calculation Sort

    In this method, a function fn() is applied to each key. The result of this function determines into which of the several sub-files the record is to be placed. The function should have the property that x <= y, fn (x) <= fn (y). Such a function is called order preserving. Thus all of the [...]...


  • Conference Call Center

    A conference call center is a centralized location where an individual, small business or large corporation can go to conduct a conference call or to allow other parties to join a conference call or listen in to a conference call. A conference call is a telephone call in which more than one party can listen [...]...