Table of Contents
Function calling in c
Pointers can be passed in functions as arguments and it is achieved via Function calling in C. This can be done via two ways i.e. Call by Value and Call by Reference in C. Let’s understand this concept with the help of a program.
The program aims to swap two numbers.
Swapping means if we have x=100 and y=200, after swapping, we get x=200 and y=100. Let’s see how to write code for such cases.
Example
#include<stdio.h> int swap (int *a, int *b) { int c; c=*a; *a=*b; *b=c; } int main () { int x, y; printf ("enter the values of x and y:\n"); scanf ("%d %d", &x, &y); printf ("before swapping values are :\n x=%d and y=%d\n", x, y); swap (&x, &y); printf ("after swapping values are :\n x=%d and y=%d\n", x, y); return 0; }
Output
Code Explanation
int swap (int *a, int *b) The function named ‘swap’ has two parameters of type ‘int’, but these arguments are the pointers. In the function definition, we have to use pointers, and while calling this function in ‘main’, we have to pass the address of two integers. Such a case is called “call by reference”.
int c; Declaring a temporary variable to perform the swapping task. c=*a;, This statement assigns ‘*a’ value to ‘c’, i.e. value at the address of ‘a’. It means ‘c’ and ‘*a’ have the same values equal to ‘*a’ which is 45.
c=45, *a=45
*a=*b; Now, ‘*b’ is assigned to ‘*a’, which means the value of ‘*b’ and ‘*a’ is equal to ‘*b’. Here, the value of ‘*a’ is changed to 98.
*a=98, *b=98
*b=c; This statement changes the value of ‘*b’, equal to ‘c’. Thus,
*b=45
The initial values of ‘*a’ and ‘*b’ were 45 and 98, respectively, but after performing the above code, the values of ‘*a’ and ‘*b’ get interchanged to 98 and 45, respectively.
Since we have done this swapping using pointers, this will target the addresses too. Due to this, the values get interchanged outside the ‘swap’ function also.
Call by reference in C
See the same code written below with minor changes.
#include<stdio.h> int swap (int *a, int *b) { int c; c=*a; *a=*b; *b=c; printf(“Swapped values in swap function are :\nx=%d and y=%d\n”,*a,*b); } int main() { int x,y; printf("enter the values of x and y:\n"); scanf("%d %d",&x,&y); printf("before swapping values are :\nx=%d and y=%d\n",x,y); swap(&x,&y); printf("Swapped values in main function are :\nx=%d and y=%d\n",x,y); return 0; }
Output:
Some extra statements are added to make the code more clear. We get interchanged values in the ‘swap’ function. But the values also get interchanged in the ‘main’ function because of the usage of pointers.
This process is call by reference and is explained below:
In the above example, we passed the address of two variables (x and y) to the swap function, and passing the address is like passing the original ‘x’ and ‘y’. The address of ‘x’ is stored in ‘a’ pointer, and that of ‘y’ is stored in ‘b’ pointer, or the function parameters ‘a’ and ‘b’ points to ‘x’ and ‘y’, respectively. Thus, any changes made in parameters ‘a’ and ‘b’ will affect the original values ‘x’ and ‘y’. Therefore, when values of ‘*a’ and ‘*b’ get interchanged in the ‘swap’ function, it results in the change of value in the ‘main’ function.
Call by Value in C
The second case of function calling is call by value.
Call by value is also known as ‘normal function call’. In this, the changes are only done in the function specified for performing the swapping task. There is no alteration in the values in the ‘main’ function. This happens because the arguments passed in the function call are the copies of the arguments passed in the function definition, and it’s like we are passing the xerox copies.
Go through the code for ‘call by value’.
#include<stdio.h> int swap (int a, int b) { int c; c=a; a=b; b=c; printf("after swapping values are %d and %d\n",a,b); } int main() { int x,y; printf("enter the values of x and y:\n"); scanf("%d %d",&x,&y); printf("before swapping values are :%d and %d\n",x,y); swap(x,y); printf("after swapping in main function\n"); printf("x=%d\n",x); printf("y=%d\n",y); return 0; }
Output:
The aim of this program is the same as done before, i.e. to interchange two values, but the difference is in the approach. Here, we use the ‘call by value’ approach. We passed ‘x’ and ‘y’ as arguments for the function call of the ‘swap’ function.
int swap (int a, int b) ‘a’ and ‘b’ are the copies of ‘x’ and ‘y’. Swap function performs its operation and interchanges values ‘a’ and ‘b’, but the values of ‘x’ and ‘y’ remain the same.
Bypassing the actual values to the function, copies of the values are passed, not the real values. That’s why the numbers got swapped inside the function, but there was no change outside it.
0 Comments