Function Call by Value and Call by Address in C

Function Call by Value and Call by Address in C Language

In this article, I will discuss Functions Call by Value and Call by Address in C Language with Examples. Please read our previous articles discussing Actual and Formal Arguments in C Language with Examples. You will understand the following pointers in detail at the end of this article.

  1. Parameter Passing Methods in C.
  2. Pass by value (or) call by value.
  3. How does Call by Value work?
  4. When do we need to use Pass by Value?
  5. Pass by address (or) call by address
  6. How does Call-By-Address work?
  7. How do you call a function in a C program?
  8. What is Function Call by Value in C?
  9. What is Function Call by Address in C?
  10. Difference Between Call by Value and Call by Address in c
How to Call a Function in a C Program?

There are two ways that a C function can be called from a program. They are,

  1. Pass by Value: The most common method in C where a copy of the actual argument’s value is passed. Changes to the formal argument do not affect the actual argument.
  2. Pass by Reference: Achieved using pointers in C. The memory address of the actual argument is passed, allowing the function to modify the original variable’s value.
Function Pass by value (or) Call by Value:

Please see the example below to understand Pass by Value or Call by Value Mechanism in C. As you can see in the example below, the main function has variables a and b with the values 10 and 20, respectively. Then, the main function calls the swap function. The swap function takes two parameters, x and y. Then, the swap function swaps the numbers x and y with the help of a temporary variable. Then, the control returns to the main function, which will print the values of a and b.

#include <stdio.h>
void swap (int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

int main ()
{
    int a, b;
    a = 10;
    b = 20;
    swap (a, b);
    printf ("%d %d", a, b);
}
How Does Function Call by Value Work in C Language?

In the main function, while calling the swap function, we pass two parameters, i.e., a and b, called actual parameters. The swap function, which takes x and y parameters, is called formal parameters.

When the main function calls the swap function, the values of a (i.e., 10) and b (i.e., 20) are copied to the x and y variables of the swap function, respectively. The formal parameters are normal variables; the swap function returns nothing, so the return type is void. Within the swap function, we have implemented the logic to swap the values of x and y variables using the temp variable.

temp = x; this will store 10 in the temp variable
x = y; this line will store 20 in the x variable
y = temp; here, it will store 10 in the y variable.

So, here, the formal parameters (x and y) are modified, but the actual parameters (a and b) remain the same. The swapping is done inside the variables of the swap function only, which are not reflected inside the actual parameters of the main function.

When the swap function completes its execution, it returns to the main function from where it is being called. Then, inside the main function, it prints the values of a and b, and you can see 10 and 20 on the console window. For a better understanding, please have a look at the below image.

Pass by value (or) call by value Parameter Passing Methods

So, in the pass-by-value mechanism, any changes are done to formal parameters that will not reflect in the actual parameter.

When do we need to use Function Pass by Value?

In C programming, “Call by Value” is a method of passing arguments to functions where the actual value of an argument is copied into a new location of memory. When the function is called, new memory space is allocated for the function parameters, and the values of the actual arguments are copied into this space. The function operates on these copied values. Here are scenarios where Call by Value is typically used or beneficial:

  • Data Protection: When you want to ensure that the function does not alter the original value of a variable. Since Call by Value creates a copy of the actual data, any modification inside the function does not affect the original data.
  • Small Data Types: For small data types like int, char, float, etc., Call by Value is efficient because copying small amounts of data does not have a significant overhead.
  • Simplicity: Call by Value is straightforward and easy to understand, making your code simpler and less prone to errors related to unintentional data modification.
  • Stateless Functions: In scenarios where functions are meant to be stateless (not retaining any change in state after execution), Call by Value is ideal since it does not modify the original data.
  • Functional Programming Approach: If your programming style is more functional (where functions do not have side effects), Call by Value aligns well with this approach.
  • Recursion: Call by Value can be preferable in recursive functions as it avoids complications arising from shared state or data.
Pass by Address (or) Call By Address Mechanism in C Language

In the Call by Address mechanism, the addresses of the actual parameters are passed to formal parameters, and the formal parameters must be pointers. Any changes done to the formal parameters will be modified with the actual parameters. Now, we need to understand how to write a call by value or address and how it works.

Note: You need to remember that Call by Address uses pointers in C.

Example to Understand Pass by Address (or) Call By Address in C Language

To understand the call by address, please look at the example below and observe the syntaxes carefully. This is the same swapping example using the call by address. As you can see in the below code, inside the main function, while calling the swap function, we are passing the addresses of the actual parameters (i.e., &a and &b), not the values (i.e., 10 and 20). This is the first change, i.e., passing the addresses. We know that pointer variables can only hold addresses. So, the second change we made is creating the formal parameters as pointer variables, i.e. (*x and *y).

#include <stdio.h>
void swap (int *x, int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int main ()
{
    int a, b;
    a = 10;
    b = 20;
    swap (&a, &b);
    printf ("%d %d", a, b);
}

Note: In call by address, the formal parameters must be pointers, and here, we have to use de-referencing, that is, *, for accessing the data of actual parameters. Here, the address will be passed in the case of actual parameters. Now, I hope the syntax is clear, and let’s proceed and understand the workflow of the call by address.

How Does Call by Address Work in C Language?

The main function has 2 variables, a and b, with values of 10 and 20, respectively. Next, the main function calls the swap function, and the addresses are passed. The *x and *y pointers will take the addresses in the swap function. So, now, the swap function can access the variables of the main function using pointers.

Note: You need to remember that one function cannot access the variables directly but can indirectly access the variables by using pointers.

Within the swap function, we have the logic to swap the values of the temp variable. Let us see how they work.

  • temp = *x; this will store 10 in the temp variable. *x will point to the actual value of the address it holds, i.e., a variable.
  • *x = *y; this line will store 20 in the “a” variable. This is because *y points to the b variable, and its value is 20, and *x points to the “a” variable.
  • *y = temp; here, it will store 10 in the b variable. This is because *y points to the b variable.

So, you can observe that the actual variables are modified. So, when the swap function ends, the control comes back, and when it prints, the value of a is 20, and the value of b is 10 is printed, which is swapped. For a better understanding, please have a look at the following diagram.

Pass by address (or) call by address mechanism

When do we need to use Function Call by Address in C?

In C programming, “Call by Address” (also known as Call by Reference) is a method of passing arguments to functions where the address (or reference) of the argument is passed instead of the actual value. This approach allows the function to access and modify the original variable. Call by Address is typically used in the following scenarios:

  • Modifying Original Data: When modifying the original variables passed to the function. Since the function receives addresses of variables, any modification to these variables within the function is reflected in the original variables.
  • Passing Large Data Structures: For large data structures like arrays, structs, or objects, passing the address is more efficient than passing a copy. It avoids the overhead of copying large amounts of data, which can improve performance and reduce memory usage.
  • Efficiency in Memory Usage: Call by Address is useful in memory-constrained environments or for performance-critical applications, as it prevents the additional memory allocation that comes with creating copies of large data structures.
  • Shared Mutable State: When multiple functions need to work on and modify the same data, passing the data address ensures that all functions have access to the latest state of the data.
  • Returning Multiple Values: C functions can return only one value. If you need a function to return multiple values, you can pass the addresses of variables in which the results will be stored.
  • Complex Data Manipulation: In scenarios where complex manipulations are performed on data, such as sorting algorithms, data structure manipulations, or complex calculations, passing by address allows direct access and modification of the data.

I hope you understand the basics of Call by Value and Call by Address. Now, let us proceed and understand the differences between these two mechanisms.

Difference Between Function Call By Value and Call By Address in C Language:

In the C programming language, understanding the difference between “call by value” and “call by address” (also known as “call by reference”) is crucial for function design and memory management. Here’s a detailed comparison:

Call by Value in C:

In call by value, the actual value of the argument is passed to the function. Key points include:

  • Copying of Values: The values of actual parameters (arguments) are copied into the function’s formal parameters (the parameters used in the function definition).
  • Memory Allocation: The copied values are stored in different memory locations. Thus, the memory address of the actual and formal parameters is different.
  • No Effect on Actual Parameters: Changes made to the formal parameters inside the function do not affect the actual parameters.
  • Use Case: Ideal when you want to protect the original data from being altered by the function.
Call by Address (Call by Reference):

In call by address, the address of the actual argument is passed to the function. Key points include:

  • Passing Addresses: Addresses of actual parameters are passed, not their values.
  • Shared Memory Location: Both the actual and formal parameters refer to the same memory location.
  • Changes Affect Actual Parameters: Modifications made to formal parameters within the function also change the actual parameters.
  • Use Case: Used when the function needs to modify the actual parameters or to pass large data structures (like arrays) efficiently.
Key Differences Between Call By Value and Call By Address in C
  • Memory Address: In call by value, actual and formal parameters are stored in different locations. In call by address, they share the same location.
  • Effect on Variables: Changes in the called function affect actual parameters in the call by address but not in the call by value.
  • Efficiency: Call by address can be more efficient for large data types, as it avoids copying entire structures or arrays.
When to Use Each
  • Call by Value: When you want to ensure the original data is not modified or when dealing with basic data types.
  • Call by Address: When you need to modify the original data or when passing large data structures for performance reasons.

In the next article, I will discuss Functions Return by Value and Return by Address in C Language with examples. I explain Function Call By Value and Call by Address in C Language with Examples in this article. I hope you enjoy this Function Call by Value and Call by Address in C Language with Examples article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *