CS-2 Call by Value ND Reference
CS-2 Call by Value ND Reference
#include<stdio.h>
void add_One(int);
main() {
int b = 10;
add_One(b);
printf(Variable b in function main() has value: %d\n, b);
}
Output
Variable num in function add_One() has value: 11
Variable b in function main() has value: 10
When the main() function call the add_One() function, it passes in the
VALUE of variable b by making a COPY of it and assigned it to the formal
parameter, num . Do not confused that the variable b is passed into the
function; only a copy of its value is passed into the function. Therefore,
whatever changes made to the num in add_One() function, does not change
the value of variable b in main() function.
Exercise
1. Whats the output?
#include<stdio.h>
void square(int);
main() {
int num = 3;
square(num);
printf(num: %d\n, num);
}
#include<stdio.h>
int square(int);
main() {
int num = 3;
square(num);
printf(num: %d\n, num);
num = square(num);
printf(num: %d\n, num);
}