In procedural oriented programming(C Language), call by value and call by reference are two types of functions.
Function:- Function is a self contained block of program which performs the predefined task.
Syntax:- Return Data Type Name of Function(Argument list);
A) Call by value type function includes
1) In call by value type function actual values of variables are passed as argument to function and that is the reason this type of function is called as call by value.
2) In function definition block these actual values are received in appropriate data type variables.
3) If any change is made in received values inside the function definition block then it does not reflect any effect on actual values in calling function.
4) Example of call by value function
int add(int, int); // declaration of function
int r,a,b;
r=add(a,b); // function call
5) Sample program for demonstration of call by value
#include
#include
void main(void)
{
int add(int, int); // declaration of function
int r,a,b;
clrscr();
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
r=add(a,b); //call to function
printf("\nvalue of a is %d\nvalue of b is %d\naddition is %d",a,b,r);
getch();
}
int add(int x, int y) //definition of function
{
int f;
f=x+y;
return(f);
}
B) Call by reference function includes
1) In call by reference type function instead of actual values of variables their addresses are passed as argument to function and that is the reason this type of function is called as call by reference.
2) In function definition block these addresses of variables are received in appropriate data type pointer variables.
3) If any change is made in received values inside the function definition block then it directly reflect on actual values in calling function.
4) Example of call by reference function
int add(int *, int *); // declaration of function
int r,a,b;
r=add(&a,&b); // function call
5) Sample program for demonstration of call by reference
#include
#include
void main(void)
{
int add(int *, int *); // declaration of function
int r,a,b;
clrscr();
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
r=add(&a,&b); //call to function
printf("\nvalue of a is %d\nvalue of b is %d\naddition is %d",a,b,r);
getch();
}
int add(int * x, int * y) //definition of function
{
int f;
f=*x + *y;
return(f);
}