In C programming language, we have different parameters passing schemes. Here we are going to discuss about the two most important ways of passing arguments to a functions such as call by value and call by reference.
Important arguments passing techniques in C
- Call by Value
- Call by Reference
Call by Value : If we call a function in C by passing values of variables as the parameters/arguments to the function then such type of function call is known as Call by value.
Call by Reference : We know that whatever variable we are using in our program, ultimately it is getting stored somewhere in memory. So instead of passing values of variables as parameters to the function, if we pass address of those variables and somehow able to access data contained inside those addresses then we will be able to call functions. Interestingly, concepts of Pointers provide us the advantage of manipulating addresses.So calling a function by passing addresses of variables as the parameters to the function is known as Call by Reference.
you can join our whats app group +919958444212 or call 01204221413 or firstname.lastname@example.org for updates on Jobs or interviews or current GD topics
Concept of Actual and Formal arguments
Arguments passed to the function during function call are known as actual arguments and the arguments we use during a function definition are known as formal arguments. For a function call to be valid the type, order and number of actual and formal arguments must always be same.
During call by value method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In this method, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.
You should also know about AMCAT
Program Code to demonstrate call by value method
int sum(int x, int y); /*function declaration*/
int num1, num2, add;
printf(” Enter any two numbers \n”);
scanf(“%d, %d”, &num1, &num2);
add = sum(num1, num2); /*Calling the add function */
int add( int x, int y) /* Function Definitions */
During call by reference, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. In this method we can alter the actual arguments by doing some changes to formal arguments.
Program code to demonstrate call by reference method
void swap(int *, int *); /*function declarations */
int a=5, b=6;
swap(&a, &b); /*Calling functions by reference //passing the address */
printf(“a=%d b=%d \n”,a,b);
void swap ( int *x, int *y) /*Function Definitions */
For questions related to functions and function calls visit the link below
Please give your valuable feedback through the comments. Visit again.