I wrote one program to understand basic of call by value in c programming. I am passing copy of parameter by value to function
value of a in main function before calling: 5
value of x in function : 5
value of x in function : 10
value of a in main function after calling: 5
If you can see the value of variable doesn't change in main function before calling and so when we call function from main function the value will be same a = 5, After calling the function the value of variable doesn't change it will still same a = 5
Does it meaningful program, can it be write in better way ?
C:
#include <stdio.h>
void function(int x);
int main(void)
{
int a = 5;
printf("value of a in main function before calling: %d\n", a);
function(a);
printf("value of a in main function after calling: %d\n", a);
return 0;
}
void function(int x)
{
printf("value of x in function : %d\n", x);
x = 10;
printf("value of x in function : %d\n", x);
}
value of x in function : 5
value of x in function : 10
value of a in main function after calling: 5
If you can see the value of variable doesn't change in main function before calling and so when we call function from main function the value will be same a = 5, After calling the function the value of variable doesn't change it will still same a = 5
Does it meaningful program, can it be write in better way ?