Answer : C
Call-By-Value : While Passing Parameters using call by value, copy of original parameter is created and passed to the called function.
Any update made inside method will not affect the original value of variable in calling function. For Example
#include<stdio.h>
void interchange(int number1,int number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}
int main() {
int num1=50,num2=70;
interchange(num1,num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
In the above example num1 and num2 are the original values and copy of these values is passed to the function and these values are copied into number1,number2 variable of interchange function respectively.
As their scope is limited to only function they cannot alter the values inside the caller function.
Call-By-Reference : This method copies the address of an argument into the formal parameter. This means that changes made to the parameter affect the original argument.
#include<stdio.h>
void interchange(int &num1,int &num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
int main() {
int num1=50,num2=70;
interchange(&num1,&num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
C uses call by value to pass arguments while C++ has both call by value and call by reference. C can simulate call by reference by passing pointers as value- but this is also technically call by value, as pointer value is copied and passed.