Function Calling in C-I

 Another concept which lies here is of Call by Value and Call by Reference these are based upon the return types.

Return type- it is a data type which returns value 

 lets see few example of it :

 

Function call by value is the default way of calling a function in C programming. Lets understand the terminologies that we will use while explaining this before we get started:

Real parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.

 


Explanation:

In the above example variable a and b are the formal parameters (or formal arguments). Variable var1 and var2 are the actual arguments (or actual parameters). The actual parameters can also be the values. Like sum(10, 20), here 10 and 20 are actual parameters.

 Function Call By value?

 
When we pass the actual parameters while calling a function then this is known as function call by value. In this case the values of actual parameters are copied to the formal parameters. Thus operations performed on the formal parameters don’t reflect in the actual parameters.

 Code:


 Output:

Working:


We passed the variable num1 while calling the method, but since we are calling the function using call by value method, only the value of num1 is copied to the formal parameter var. Thus change made to the var doesn’t reflect in the num1.

 


You might have a Question here right ?

values didn't changed but why??

Explanation:


The reason is same – function is called by value for num1 & num2. So actually var1 and var2 gets swapped (not num1 & num2). As in call by value actual parameters are just copied into the formal parameters.


Comments