Thursday, 4 April 2019

Functions in C(Part 2-Parameter Passing Methods)

C programming
In the previous article, you learnt basics of functions in C. In this article, you will learn about methods of passing values.

Passing values to function
There are two ways to pass arguments/parameters to function calls -- call by value and call by reference.
(i). Pass by value
(ii). Pass by reference

(i).Pass by Value
  In this method of parameter passing, a copy of actual parameters is passed to respective formal parameters. Any changes done in formal parameters are not reflected in actual parameters.
When a function is called, any arguments that are provided by the caller are simply treated as expressions. The value of each expression has the appropriate conversions applied and is then used to initialize the corresponding formal parameter in the called function, which behaves in exactly the same way as any other local variables in the function.
The initialization of the formal parameters is the last time that any communication occurs between the caller and the called function, except for the return value.

Example:
//Following example shows the functioning of call by value method
#include<stdio.h>

void swapCallByValue(int,int);  //function prototype
int main(){
    int num1=10,num2=20;
   printf("Values before swapping by call by value: num1=%d,num2=%d.\n",num1,num2);
    swapCallByValue (num1,num2);
    printf("Values after swapping by call by value: num1=%d,num2=%d.\n\n",num1,num2);
    return 0;
}
//function definition: using third variable
void swapCallByValue (int n1,int n2){
  int temp;
  temp=n1;
  n1=n2;
  n2=temp;
  printf("Inside swapCallByValue : num1=%d,num2=%d\n",n1,n2);
}
Output:
   Values before swapping by call by value: num1=10,num2=20.
   Inside swapCallByValue : num1=20,num2=10
   Values after swapping by call by value: num1=10,num2=20.

(ii).Pass by Reference
In call by reference method of parameter passing, the memory address of actual parameters is passed to formal parameters, hence any change made to formal parameters will also reflected in actual parameters.

This method of parameter passing is use-
(a).If the size of data is large.
(b).When there are more than one value we want to alter.

Example:
//Following example shows the functioning of call by reference method
#include<stdio.h>
void swapCallByReference(int *,int *);  //function prototype
int main(){
    int num1=10,num2=20;
    printf("Values before swapping by call by reference: num1=%d,num2=%d.\n",num1,num2);
    swapCallByReference (&num1,&num2);
    printf("Values after swapping by call by reference: num1=%d,num2=%d.\n\n",num1,num2);
    return 0;
}
//function definition: using third variable
void swapCallByReference (int *n1,int *n2){
  int temp;
  temp=*n1;
  *n1=*n2;
  *n2=temp;
  printf("Inside swapCallByReference : num1=%d,num2=%d\n",*n1,*n2);
}
Output:
   Values before swapping by call by reference: num1=10,num2=20.
   Inside swapCallByReference : num1=20,num2=10
   Values after swapping by call by reference: num1=20,num2=10.

Points to note about Functions
(a).The name of a function is always followed by ( ).
(b).The keyword void is used in the example to show the function has no return
value and no parameters.
(c).When no return type is specified, it is assumed to be int.
(d).The main program is also a function. It is called the driver function.
(e).The program always starts at the beginning of main, regardless of whether it is
the first function defined or not.


Please comment and share if you find it useful.

No comments:

Post a Comment