Understanding Call by Value vs Pointer/Address in Linux C Programming

In C programming language, you can call a function in two primary ways: call by value and call by pointer or address. Let’s delve into these concepts with straightforward examples for clarity.

Consider writing a program to swap two values. Here’s a simple implementation:

#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;
}

And the output, upon entering values 5 and 9, looks like this:

Enter two integer values
5 9

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

Now, suppose the requirement is to encapsulate the swapping logic within a separate function named swap, which can be invoked whenever needed. Here’s how that can be done:

#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;
}

Here, we have created a separate swap function, which is passed the values entered by the user from the main function. The method used here is known as ‘call by value’. The function processes the copies of provided arguments, ensuring the original variables remain unchanged. To actually swap the contents of a and b, call by pointer or address must be employed.

For modifying the original variables, instead of passing their values, their addresses should be passed. This is achievable through pointers, which we will discuss comprehensively in a future tutorial. For now, know that pointers store memory addresses.

An integer pointer declaration looks like this:

int *x;

If i is an integer variable, you can store its address in x as follows:

x = &i;

By dereferencing, you can access or modify i‘s value. Here’s an example to set i to 10:

*x = 10;

Armed with this knowledge, here’s how to 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;
}

Instead of passing the values directly, we pass the addresses, enabling the function to update the actual variables. Here’s the resulting output:

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

Conclusion

This tutorial provided insight into ‘call by value’ and ‘call by address/pointer’ in C function calls. Experiment with these examples and do not hesitate to post questions or comments if clarification is needed.

FAQ

  • What is the main difference between call by value and call by address?In call by value, copies of the actual parameters are passed, whereas in call by address, the memory addresses are passed, allowing the function to directly modify the original variables.
  • Why are pointers necessary for call by address?Pointers hold the memory addresses of variables, enabling functions to access and manipulate the data stored at those addresses directly.
  • Can swapping be done without pointers?Yes, but without pointers, the changes won’t affect the original variables, as demonstrated in the ‘call by value’ example.
  • When should I use call by value or call by address?Use call by value when the original data should remain unchanged and call by address when you need to modify the original data.