Functions part2

1) Function – Call by value method – In the call by value method the actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters.

2) Function – Call by reference method – Unlike call by value, in this method, address of actual arguments (or parameters) is passed to the formal parameters, which means any operation performed on formal parameters affects the value of actual parameter

Function call by reference in C Programming

Before we discuss function call by reference, lets understand the terminologies that we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.
For example: We have a function declaration like this:

int sum(int a, int b);

The a and b parameters are formal parameters.

We are calling the function like this:

int s = sum(10, 20); //Here 10 and 20 are actual parameters
or 
int s = sum(n1, n2); //Here n1 and n2 are actual parameters

In this guide, we will discuss function call by reference method. If you want to read call by value method then refer this guide: function call by value.


Lets get back to the point.

What is Function Call By Reference?

When we call a function by passing the addresses of actual parameters then this way of calling the function is known as call by reference. In call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters. It may sound confusing first but the following example would clear your doubts.

Example of Function call by Reference

Lets take a simple example. Read the comments in the following program.

#include <stdio.h>
void increment(int  *var)
{
    /* Although we are performing the increment on variable
     * var, however the var is a pointer that holds the address
     * of variable num, which means the increment is actually done
     * on the address where value of num is stored.
     */
    *var = *var+1;
}
int main()
{
     int num=20;
     /* This way of calling the function is known as call by
      * reference. Instead of passing the variable num, we are
      * passing the address of variable num
      */
     increment(&num);
     printf("Value of num is: %d", num);
   return 0;
}

Output:

Value of num is: 21

Example 2: Function Call by Reference – Swapping numbers

Here we are swapping the numbers using call by reference. As you can see the values of the variables have been changed after calling the swapnum() function because the swap happened on the addresses of the variables num1 and num2.

#include 
void swapnum ( int *var1, int *var2 )
{
   int tempnum ;
   tempnum = *var1 ;
   *var1 = *var2 ;
   *var2 = tempnum ;
}
int main( )
{
   int num1 = 35, num2 = 45 ;
   printf("Before swapping:");
   printf("\nnum1 value is %d", num1);
   printf("\nnum2 value is %d", num2);

   /*calling swap function*/
   swapnum( &num1, &num2 );

   printf("\nAfter swapping:");
   printf("\nnum1 value is %d", num1);
   printf("\nnum2 value is %d", num2);
   return 0;
}

Output:

Before swapping:
num1 value is 35
num2 value is 45
After swapping:
num1 value is 45
num2 value is 35

Function call by value in C programming

Function call by value is the default way of calling a function in C programming. Before we discuss function call by value, lets understand the terminologies that we will use while explaining this:

Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.

For example:

#include <stdio.h>
int sum(int a, int b)
{
     int c=a+b;
     return c;
}

int main(
{
    int var1 =10;
    int var2 = 20;
    int var3 = sum(var1, var2);
    printf("%d", var3);

    return 0;
}

In the above example variable a and b are the formal parameters (or formal arguments). Variable var1 and var2 are the actual arguments (or actual parameters). The actual parameters can also be the values. Like sum(10, 20), here 10 and 20 are actual parameters.

What is Function Call By value?
When we pass the actual parameters while calling a function then this is known as function call by value. In this case the values of actual parameters are copied to the formal parameters. Thus operations performed on the formal parameters don’t reflect in the actual parameters.

Example of Function call by Value

As mentioned above, in the call by value the actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters. Lets take an example to understand this:

#include <stdio.h> int increment(int var) { var = var+1; return var; } int main() { int num1=20; int num2 = increment(num1); printf("num1 value is: %d", num1); printf("\nnum2 value is: %d", num2); return 0; }

Output:

num1 value is: 20 num2 value is: 21

Explanation
We passed the variable num1 while calling the method, but since we are calling the function using call by value method, only the value of num1 is copied to the formal parameter var. Thus change made to the var doesn’t reflect in the num1.

Example 2: Swapping numbers using Function Call by Value

#include <stdio.h>
void swapnum( int var1, int var2 ) {
int tempnum ; /*Copying var1 value into temporary variable */
tempnum = var1 ; /* Copying var2 value into var1*/
var1 = var2 ; /*Copying temporary variable value into var2 */
var2 = tempnum ; }
int main( ) {
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2); /*calling swap function*/
swapnum(num1, num2);
printf("\nAfter swapping: %d, %d", num1, num2); }

Output:

Before swapping: 35, 45 After swapping: 35, 45

Why variables remain unchanged even after the swap?
The reason is same – function is called by value for num1 & num2. So actually var1 and var2 gets swapped (not num1 & num2). As in call by value actual parameters are just copied into the formal parameters.





Comments