Calling A Function
Calling A Function
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.
In the above example, the function call is made using addNumbers(n1, n2); statement inside the
main() function.
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.
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
Function Calling in C (call by value and call by reference)
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference in C
Call by reference method copies the address of an argument into the formal parameter. In this
method, the address is used to access the actual argument used in the function call. It means that
changes made in the parameter alter the passing argument.
In this method, the memory allocation is the same as the actual parameters. All the operations in
the function are performed on the value stored at the address of the actual parameter, and the
modified value will be stored at the same address. Means, both the actual and formal parameters
refer to same locations, so any changes made inside the function are actually reflected in actual
parameters of caller.
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);
10. }
11. void swap (int *a, int *b)
12. {
13. int temp;
14. temp = *a;
15. *a=*b;
16. *b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",*a,*b);
18. }
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Actual and formal arguments are created at Actual and formal arguments are created at the
the different memory location same memory location