Call By Value:-
call by value passes the copy of the variable to the function that process itLet’s take a look at a call by value example:
#include <stdio.h>
#include<conio.h>
void call_by_value(int x)
{
printf("
Before adding 10
x = %d .\n", x);
x=x+10;
printf("
After adding 10
x = %d.\n", x);
}
void main()
{
int a=10;
clrscr();
printf("a = %d before function call_by_val.\n",a);
call_by_value(a);
printf("a = %d after function call_by_val.\n",a);
getch();
}
Call By Reference:-
call by reference passes the memory reference of the variable
(pointer)
to the function.
to the function.
Let’s take a look at a code example:
#include <stdio.h>
#include<conio.h>
void call_by_reference(int *y)
{
printf("
Before adding 10
y = %d .\n", *y);
(*y) += 10;
printf("
After adding 10
y = %d .\n", *y);
}
void main()
{
int b=10;
clrscr();
printf("b = %d before function call_by_ref.\n",b);
call_by_reference(&b);
printf("b = %d after function call_by_ref.\n",b);
}
No comments:
Post a Comment