Linux C Programming Tutorial Part 8 - Call by Value Vs Call by Pointer/Address

In our first part of this ongoing tutorial series, we discussed briefly about functions, including their declaration and body. What we didn't discuss at that time was the multiple ways in which functions are called. Here, in this tutorial, we will quickly discuss the ways that exist.

In C language, you can call a function in a couple of ways: call by value and call by pointer or address. Let's discuss both these concepts with some easy to understand examples.

Let's say you want to write a program that swaps two values. Here's how you can do it:

#include <stdio.h>

int main()
{
int a=0, b=0, c=0;

printf("Enter two integer values\n");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

c = a;
a = b;
b = c;

printf("\nSwapped values are: %d and %d", a,b);



return 0;
}

Here's the output of this code with entered values as 5 and 9:

Enter two integer values
5 9

Entered values are: 5 and 9
Swapped values are: 9 and 5

Now, suppose the requirement is to have a separate function - say 'swap' - which does all the swapping related work and can be called whenever the programmer wants to swap two values. Following is the code that does this:

#include <stdio.h>

void swap (int val1, int val2)
{
int temp = 0;

temp = val1;
val1 = val2;
val2 = temp;

printf("\nSwapped values are: %d and %d", val1,val2);

}

int main()
{
int a=0, b=0;

printf("Enter two integer values\n");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(a,b);

return 0;
}

So there you go. A separate function named 'swap' has been created that receives two values (originally entered by user and captured in 'main' function) as arguments and then swaps them and prints the output. 

The way 'swap' has been called here is known as 'call by value'. Reason being, when the call is made, only the values held by 'a' and 'b' are passed to the 'swap' function as arguments. These values are received by arguments 'val1' and 'val2', and it's these variables on which the swap process is performed. 

This means that variables 'a' and 'b' in the 'main' function continue to hold original values even after the swap operation has been performed. But what if the requirement is to have values of 'a' and 'b' swapped after calling the 'swap' function? Well, this is where the 'call by pointer/address' method comes into picture.

So basically what we do here is, we pass the address of variables (like 'a' and 'b' in our case) as arguments. The functionwhich is called ('swap' in this case) is equipped to receive addresses as arguments and then the swap process is done on the values kept at these addresses, which effectively means values of original variables ('a' and 'b' here) get swapped.

Now, in the previous paragraph, we said the function "is equipped to receive addresses as arguments." Well, by "equipped," we meant it has special type of arguments that can receive addresses. These arguments are 'pointer' type variables. We will discuss 'pointers' in detail in an upcoming tutorial, but for now, just keep in mind that pointer variables store memory addresses as values.

Here's how a pointer to an integer is declared/defined:

int *x;

So basically, x is a pointer variable that can be used to store memory address of an integer variable. Suppose 'i' is an integer variable, then here's how you can make 'x' store the address of 'i':

x = &i;

And whenever you want to access the value of 'i' through 'x', you write '*x'. For example, here's how you can change the value of 'i' to, say, 10:

*x = 10;

So with all this in mind, here's how you can call 'swap' using the call by address or pointer method:

#include <stdio.h>

void swap (int *val1, int *val2)
{
int temp = 0;

temp = *val1;
*val1 = *val2;
*val2 = temp;

}

int main()
{
int a=0, b=0, c=0;

printf("Enter two integer values\n");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(&a,&b);

printf("\nSwapped values are: %d and %d", a,b);


return 0;
}

So this time, instead of passing values of 'a' and 'b' as arguments, we passed the addresses of these variables. In 'swap' function, the addresses are received in two pointer variables ('val1' and 'val2'). And using the two pointer variables, the logic directly swaps values of 'a' and 'b'. 

Here's the output:

Enter two integer values 
6 8
Entered values are: 6 and 8
Swapped values are: 8 and 6

Conclusion

This article should've given you at least a basic idea of what 'call by value' and 'call by address/pointer' ways of function calling are, and when they can be used. Try out the examples we've listed here and let us know in comments below if you have any doubt or query.

Share this page:

0 Comment(s)