Types of Function calls in C (or) Parameter passing techniques - C-Tutorial

Latest

Friday, 11 November 2016

Types of Function calls in C (or) Parameter passing techniques

Types of Function calls in C (or) Parameter passing techniques

Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them,

1. Call by Value
2. Call by Reference

1. Call by Value: 
In this calling technique we pass the values of arguments which are stored or copied into the formal parameters of functions. Hence, the original values are unchanged only the parameters inside function changes.

#include<stdio.h>
#include<conio.h>

void swap(int, int);
int main()
{
 int x = 10, y=20;
printf("\n Before swapping the X=%d, Y=%d",x,y);
swap(x,y);
printf("\n After swapping the X=%d, Y=%d",x,y);
}

void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

In this case if we made any changes with receiving arguments, Those changes are not reflect on outside of the function


2. Call by Reference (or) Call by address:

In this we pass the address of the variable as arguments. In this case the formal parameter can be taken as a reference or a pointer, in both the case they will change the values of the original variable.

#include<stdio.h>
#include<conio.h>

void swap(int *, int *);
int main()
{
 int x = 10, y=20;
printf("\n Before swapping the X=%d, Y=%d",x,y);
swap(&x, &y);
printf("\n After swapping the X=%d, Y=%d",x,y);
}

void swap(int *x, int *y)
{
int temp;
temp= *x;
*x = *y;
*y = temp;
}

In this case if we made any changes with receiving arguments, Those changes are reflect on outside of the function

No comments:

Post a Comment