Functions in 'C' (Part 2)

Calling Function: Function can be called in two ways as follow

1) Call by Value : 

  • In this method we pass actual parameters or direct values to function while calling it.
  • As seen in previous article.
  • Example : Consider program for multiplication of two integers using function
    #include<stdio.h>
    int main()
    {
       int a=5,b=8;
       void  swap(int a,int b)
       {
            int temp = 0;
             temp = a;
             a = b;
             b = temp;
        }
        printf("Before calling function : \n");
        printf("a=%d and b=%d",a,b);
        swap(a,b);
        printf("After calling function : \n");

        printf("a=%d and b=%d",a,b);

     return 0;
   }

Output :

Before calling function :
a=5 and b=8After calling function :
a=5 and b=8


Note : 
  • When we call function by passing a,b values of a,b gets copied into n,m and function is executed. Thus called as call by value.
  • Here actual variable does get affected by function since function is not changing actual variables instead it copy them into formal variables and operate on formal variables.
  • Thus both printf before and after calling function will print a=5 and b=8. 

2) Call by Reference : 
  • Reference is nothing but address of memory location for a variable. 
  • So in this method we pass reference or address of variables to function while calling it.
  • As we are passing reference function will operate on actual variables and thus they may get changed after calling function.
  • Reference is indicated by &var_name. & is also known as address of operator since it returns address of variable.
  • Remember that in this method function definition must contain arguments as pointer to variable. Indicated by *var_name.
  • * refers to content of.
  • Let's see example :
#include<stdio.h>
int main ()
{
   int a=5,b=8;
   void swap(int *a,int *b)
    {
        int temp=0;
        temp = *a;
         *a=*b;
        *b = temp;
     }
   printf("Before calling function:\n");
   printf("a=%d and b=%d",a,b);
   swap(&a,&b);
   printf("\n After calling function:\n);

   printf("a=%d and b=%d",a,b);
 return 0;
}

Output:

Before calling function:
a=5 and b=8

After calling function:
a=8 and b=5




Note : 
  • Here while function execution *a,*b becomes *(&a),*(&b). 
  • Let's assume that value of a that is 5 stored at address101 and values at b that is 8 is stored at address 105.
  • Thus above becomes *(101),*(105) referring to actual value 5 and 8.
  • After execution of function actual values will get swapped or exchanged because of reference.
  • Thus before calling function output will be a=5 and b=8 whereas after calling function output will be a=8 and b=5. This is difference between two methods of calling function.

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.