Thursday, January 16, 2025
HomeProgrammingCall by Value and Call by Reference in C

Call by Value and Call by Reference in C

In C programming, Call by Value and Call by Reference are two methods used to pass arguments to functions. Here’s a breakdown of each:

1. Call by Value:

In Call by Value, the actual value of the argument is passed to the function. This means the function works on a copy of the argument, and any changes made to the parameter inside the function do not affect the actual argument outside the function.

How it works:

  • When a function is called, the values of the arguments are copied into the function’s parameters.
  • Changes made to the parameter inside the function affect only the copy, not the original value.

Example of Call by Value:

#include <stdio.h>

void addTen(int num) {
    num = num + 10; // num is a local copy of the argument
    printf("Inside function: %d\n", num);
}

int main() {
    int x = 5;
    addTen(x);
    printf("Outside function: %d\n", x);  // Original value of x remains unchanged
    return 0;
}

Output:

Inside function: 15
Outside function: 5
  • Explanation: The value of x in the main function remains 5 because the function addTen only modifies the local copy of x, not the original variable.
See also  How to Find All Files Containing a Specific Text (String) on Linux

2. Call by Reference:

In Call by Reference, the address (or reference) of the argument is passed to the function. This means the function can modify the actual value of the argument, because it is working directly with the original data stored at the memory address.

How it works:

  • The function receives the address of the argument (i.e., a pointer to the variable).
  • Any changes made to the parameter inside the function directly affect the original variable outside the function.

Example of Call by Reference:

#include <stdio.h>

void addTen(int *num) {
    *num = *num + 10;  // Dereferencing the pointer to modify the original value
    printf("Inside function: %d\n", *num);
}

int main() {
    int x = 5;
    addTen(&x);  // Passing the address of x
    printf("Outside function: %d\n", x);  // The value of x is changed
    return 0;
}

Output:

Inside function: 15
Outside function: 15
  • Explanation: In this case, the address of x is passed to the function. Inside the function, we dereference the pointer (*num) to modify the actual value of x in the main function. As a result, x becomes 15 both inside and outside the function.
See also  How to calculate percentage with a SQL statement

Key Differences between Call by Value and Call by Reference:

Aspect Call by Value Call by Reference
What is passed A copy of the value of the argument. The address (reference) of the argument.
Effect on the argument The original argument is not modified. The original argument can be modified.
Memory used Uses more memory because a copy of the variable is created. Uses less memory because only a reference (address) is passed.
Default in C Call by Value is the default in C. Call by Reference requires passing the address using pointers.
Use Case Safe when you don’t want to change the original value. Used when you need to modify the original data inside the function.
See also  How can I check if a variable is an integer?

Conclusion:

  • Call by Value is used when you want the function to work on a copy of the data, without modifying the original.
  • Call by Reference is used when you want the function to modify the actual argument, directly affecting the original data.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x