Coupon Accepted Successfully!


Passing Arguments

The main objective of passing argument to function is message passing. The message passing is also known as communication between two functions, that is between caller and called functions. There are three methods by which we can pass values to the function. These methods are as follows:

  1. Call by value (pass by value)
  2. Call by address (pass by address)
  3. Call by reference (pass by reference)

C++ supports all these three types of passing values to the function, whereas C supports the first two types only. The arguments used to send values to function are known as input arguments. The arguments used to return result are known as output arguments. The arguments used to send as well as return results are known as input–output arguments. While passing values to the function, the following conditions should be fulfilled.

The data type and number of actual and formal arguments should be same both in caller and callee functions. Extra arguments are discarded if they are declared. If the formal arguments are more than the actual arguments, then the extra arguments appear as garbage. Any mismatch in the data type will produce the unexpected result.

Call by Value

In this type, values of actual arguments are passed to the formal arguments and operation is done on the formal arguments. Any change in the formal arguments does not effect to the actual arguments because formal arguments are photocopy of actual arguments. Hence, when function is called by call by value method, it does not affect the actual contents of actual arguments. Changes made in the formal arguments are local to the block of called function. Once control returns back to the calling function, the changes made will vanish. The advantage of this call by value is that actual parameters are fully protected because their values are not changed when control is returned to the calling function. The following example illustrates the use of call by value.

7.3 Write a program to demonstrate call by value.


Explanation: In the above program we are passing values of actual arguments ‘x’ and ‘y’ to function swap(). The formal arguments ‘a’ and ‘b’ of function swap() receive these values. The values are exchanged i.e. value of ‘a’ is assigned to ‘b’ and vice-versa. They are displayed on the screen. When the control returns back to the main(), the changes made in function swap() vanish, because a and b are local variables of function swap(). In the main() the values of ‘x’ and ‘y’ are printed as they read from the keyboard. In call by value method the formal argument acts as duplicate of the actual argument. The addresses of actual and formal arguments are different. Thus, changes made with the variables are temporary.

Call by Address

In this type, instead of passing values, addresses of actual parameters are passed to the function by using pointers. Function operates on addresses rather than values. Here the formal arguments are pointers to the actual arguments. In this type, formal arguments are pointers to actual argument. Because of this, when the values of formal arguments are changed, the values of actual parameters also change. Hence changes made in the argument are permanent. The following example illustrates passing the arguments to the function using call by address method.

7.4 Write a program to demonstrate pass by address.

Explanation: In the above example we are passing addresses of actual arguments to the function swap(). The formal arguments are declared as pointers in the function declaration of swap(). The formal arguments receive addresses of actual arguments i.e. formal arguments points to the actual argument. Here the swap() function operates on the addresses of actual argument through pointers. The addresses of actual arguments and formal arguments are same. Hence, the changes made in the values are permanent.

Call by Reference

C passes arguments by value and address. In C++ it is possible to pass arguments by value, address, and reference. C++ reference types, declared with ‘&’ operator, are nearly identical but not exactly the same as pointer types. They declare aliases for objects variables and allow the programmer to pass arguments by reference to functions. The reference decelerator (&) can be used to declare references outside functions.


This creates the lvalue kk as an alias (assumed name) for k, provided that the initializer is the same type as the reference. Any operations on kk have exactly the same result as operations on k.

The reference decelerator can also be used to declare reference type parameters within a function:

The s argument passed by reference can be modified directly by funcB; whereas the function funcA receives a duplicate copy (not actual) of the s argument (passed by value). Due to this reason, variable s itself cannot be modified by funcA. When an actual variable s is not appearing in the above example, argument s is passed by value, and the matching formal argument in the function obtains a copy of s. Any alterations to this copy inside the scope of the function body are not thrown back in the value of s itself. Absolutely, the function can return a value that could be used later to change s, but the function cannot directly alter a parameter passed by value. The conventional C method for changing s operates on the address of actual argument (&s), the address of s, rather than s itself. Although &s is passed by value, the function can access s through the copy of &s it receives. Even though the function does not need to alter s, it is still useful (though subject to possibly risky secondary results) to pass &s, especially if s is a large data structure. Passing s directly by value contains the useless copying of the data structure.

7.5 Write a program to pass the value of variable by value, reference, and address and display the result.

Explanation: In the above example, funA(), funB(), and funC() functions are declared. The integer variable s is declared and initialized to 4. The funA() is invoked and value of s is passed by value. In function funA() the value of s is incremented. After execution of funA() the value of s printed in main() is the same as the previous one. Thus, passing variable by value cannot change the contents of the variable.

The value of s is once again passed to funB(). The function funB() receives the value of s by reference. The value of s is received by the variable k by reference, that is variable k is an alias for variable s and both have the same memory location. The variable k is incremented. Thus, any change made in reference variable affects the actual variable. Thus, after execution of funB() the printed value of s is 5.

The third function funC() uses conventional C style. Here, address of the variable s is passed to funC(). The formal argument *j is pointer to the actual argument. Thus, any change made through pointer j also reflects on the actual variable. Thus, we can change the value of variable using call by reference. Figure explains the methods of passing arguments to function.

Argument passing methods

7.6 Write a program to demonstrate call by reference.

Explanation: In the above program, the function swap() is invoked and actual arguments, variable x and y, are passed. The formal arguments of function swap(), a and b, are reference variables of actual arguments x and y. The addresses of actual and formal arguments are same. Any change made in a and b leads to change in the values of x and y. To pass variables by reference, the function call is like call by value. In function declarator and function prototype declaration, the formal arguments are preceded by ‘&’ operator. The reference type variable can be used in the same manner like other ordinary variables. The following points are noted concerning reference parameters:

  1. A reference may not be null. It should always refer to a legal variable.
  2. Once declared, a reference should not be altered to referrer to another object.
  3. A reference variable does not need any explicit address manipulation to access the actual value of the variable.

Test Your Skills Now!
Take a Quiz now
Reviewer Name