Call by value and call by reference in C
call by value-
#include<stdio.h>
void add(int n); //function declaration
int main()
{
int num =2;
printf("\n The value of num before the function is %d",num);
add(num); //function call
printf("\n The value of num after calling the function =%d",num);
return 0;
}
// function difination
void add(int n) //function header
{
n=n + 10;
printf("\n The value of num in the called function =%d",n);
}
Output-
The value of num before the function is 2
The value of num in the called function =12
The value of num after calling the function =2
call by reference-
#include<stdio.h>
void add(int *n); // function declaration
int main()
{
int num =2;
printf("\n The value of num before calling the function =%d",num);
add(&num);
printf("\n The value after calling the function =%d",num);
return 0;
}
//function defination
void add(int *n) //function header
{
n=*n + 10;
printf("\n The value of num in the called function =%d",n);
}
output-
The value of num before calling the function =2
The value of num in the called function =12
The value after calling the function =2
#include<stdio.h>
void add(int n); //function declaration
int main()
{
int num =2;
printf("\n The value of num before the function is %d",num);
add(num); //function call
printf("\n The value of num after calling the function =%d",num);
return 0;
}
// function difination
void add(int n) //function header
{
n=n + 10;
printf("\n The value of num in the called function =%d",n);
}
Output-
The value of num before the function is 2
The value of num in the called function =12
The value of num after calling the function =2
call by reference-
#include<stdio.h>
void add(int *n); // function declaration
int main()
{
int num =2;
printf("\n The value of num before calling the function =%d",num);
add(&num);
printf("\n The value after calling the function =%d",num);
return 0;
}
//function defination
void add(int *n) //function header
{
n=*n + 10;
printf("\n The value of num in the called function =%d",n);
}
output-
The value of num before calling the function =2
The value of num in the called function =12
The value after calling the function =2
Comments
Post a Comment