Answer: While constructing a function the parameters can be passed by value or by reference.
In value parameter we actual make a copy of the values which has been passed to the function from the main method while we reference parameter we copy the address of the values in memory so that while our program references those values it it can refer them from their memory location.
Step-by-step explanation:
Now we take can example to show parameter passing by value and reference with the help of an C program to add two numbers
int add_val(int x, int y) {
return x + y;
}
void add_ref(int *x, int y) {
*x += y;
}
int main () {
int a, b, c, d;
a = 2;
b = 1;
c = add_val(a,b); //value parameter
d = add_ref(&a, c); // reference parameter
return 0;
}
In the above code the value the value of c is 3 and the value of d is 5. In add_p we have passed the address of the a but in add_val we have passed the values of a and b.