As I explained in the previous chapter about the different types of function calls.On the basis of input parameter passed into the Function, “Function calling with input parameters” again divided into two category –
— Function Call by value
— Function Call by reference
Every initialized variable has some address in memory to keep its data.In call by value function, we have to pass the variable name or its value while in the call by reference we have to send the address of variable as an input parameter.
In function Call by value, Input parameters are variables or some constants.
1 |
function_Name(input1,input2); |
1 2 3 |
1. func1(2,3); /*input parameter are Integer constants*/ 2. func2(x,y); /*input parameters are variables which hold some constants*/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<stdio.h> /*function prototype declaration*/ unsigned int addition(unsigned int ,unsigned int ); int main() { unsigned int num1; unsigned int num2; unsigned int sum; printf("\nProgram to add two unsigned integers :"); printf("\nEnter Num1 :"); scanf("%d",&num1); printf("\nEnter Num2 :"); scanf("%d",&num1); sum = addition(num1,num2); /*function call by value*/ printf("sum=%u",sum); return 0; } /*function definition*/ unsigned int addition(unsigned int num1_copy,unsigned int num2_copy) { unsigned int sum; sum=num1_copy+num2_copy; return sum; } |
— Function prototype must be declared before its calling.
— In function call by value operation not performs on real(num1,num2) data but on its copy(num1_copy,num2_copy).
–The main function is the entry and exit point for any program in C.so we can call any self-defined function from main but the pointer will return back to the immediate statement of calling function because the exit of the program exists only in the main function.(see the third number in above flow).
In function Call by reference, Input parameters shall be the address of variables or constant in function calling.
To understand this topic you must be familiar with the pointer concept first.
1 |
function_Name(&input1,&input2); |
1 2 3 |
1. func1(&2,&3); /*input parameters address*/ 2. func2(&x,&y); /*input parameters address*/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<stdio.h> unsigned int addition(unsigned int *,unsigned int *); /*function prototype declaration*/ int main() { unsigned int num1; unsigned int num2; unsigned int sum; printf("\nProgram to add two unsigned integers :"); printf("\nEnter Num1 :"); scanf("%d",&num1); printf("\nEnter Num2 :"); scanf("%d",&num1); sum = addition(&num1,&num2); /*function call by value*/ printf("sum=%u",sum); return 0; } /*function definition*/ unsigned int addition(unsigned int *num1_Address,unsigned int *num2_Address) { unsigned int sum; sum=*num1_copy+*num2_copy; return sum; } |
In function call by reference, Operation performs on real(num1,num2) data because input parameters are nothing but the address of the actual variable inside the calling function. So if we do any changes to that address, it means we are doing this with the original value.