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

Leave a Comment

Your email address will not be published. Required fields are marked *

Hire a server Expert to resolve the issue Now.

Hire a server Expert to resolve the issue Now.

Resolve this issue in just 5$ from https://serverexpert.io

 

You have Successfully Subscribed!

Open chat