Call
by value
è In this kind of
function, pass the value directly to the called function through the calling
function. The called function does process and returns the value back to the
calling function.
è In this method the
value of each of actual argument in calling function is copied into
corresponding formal arguments of the called function.
è Any change made to the arguments internally in the
function are made only to the local copies of the arguments, It will not reflect to actual argument in
calling function.
Ex,
#include < stdio.h>
void swap(int a, int b);
void main()
{
int a, b;
a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);
swap(a, b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(" \n Inside function swap :");
printf(" a = %d, b = %d", a, b);
}
O/P
From main: a = 5, b = 7
Inside function swap :a = 7, b = 5
Back in main: a = 5, b = 7
Call By Reference
è In call by reference ,
not need to pass value directly to the called function , Instead of that pass the address of the variable which
hold the value. It can be got by passing the address to the function.
è By this method , change made to the parameters of function will affect the variables which
called the function.
#include < stdio.h>
void swap(int *a, int *b);
void main()
{
int a, b;
a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);
swap(&a,&b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf(" Inside function swap ");
printf("a = %d, b = %d\n", *a, *b);
}
O/P
From main: a = 5, b = 7
Inside function swap :a = 7, b = 5
Back in main: a = 7, b = 5
Posted By : Ruchita Pandya
No comments:
Post a Comment