Calling a function
When a program calls a function, the program control is transferred to the called function. A
called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main
program.
To call a function, you simply need to pass the required parameters along with the function
name, and if the function returns a value, then you can store the returned value.
Control of the program is transferred to the user-defined function by calling it.
Syntax of function call
functionName(argument1, argument2, ...);
In the above example, the function call is made using addNumbers(n1, n2); statement inside the
main() function.
Call by Value and Call by Reference
Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways
are generally differentiated by the type of values passed to them as parameters.
The parameters passed to function are called actual parameters whereas the parameters received
by function are called formal parameters.
Call By Value in C: In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different memory
locations. So any changes made inside functions are not reflected in actual parameters of caller.
In other words, in this parameter passing method, values of actual parameters are copied to
function's formal parameters, and the parameters are stored in different memory locations. So
any changes made inside functions are not reflected in actual parameters of the caller.
Call by Value Example: Swapping the values of the two variables
1. #include <stdio.h>
2. void swap(int , int); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b);
8. swap(a,b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); }
By: Pragya Singh Tomar
Page 1 ICS,Vikram University, Ujjain