Call by Value and Call by Reference in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In C, a function specifies the modes of parameter passing to it. There are two ways to specify function calls: call by value and call by reference in C. In call by value, the function parameters gets the copy of actual parameters which means changes made in function parameters did not reflect in actual parameters. In call by reference, the function parameter gets reference of actual parameter which means they point to similar storage space and changes made in function parameters will reflect in actual parameters.

Before reading this article read the following C programming topics:

Introduction

Suppose you have a file and someone wants the information present in the file. So to protect from alteration in the original file, you give a copy of your file to them and if you want the changes done by someone else in your file then you have to give them your original file. In C also, if we want the changes done by function to reflect in the original parameters also, then we pass the parameter by reference, and if we don't want the changes in the original parameter, then we pass the parameters by value. We get to know about both call by value and call by reference in c and their differences in upcoming sections.

Difference Between Call by Value and Call by Reference in C

Calling by ValueCalling by Reference
Copies the value of an object.Pass a pointer that contains the memory address of an object that gives access to its contents.
Guarantees that changes that alter the state of the parameter will only affect the named parameter bounded by the scope of the function.Changes that alter the state of the parameter will reflect to the contents of the passed object.
Simpler to implement and simpler to reason with.More difficult to keep track of changing values that happens for each time a function may be called.

Call by Value in C

Calling a function by value will cause the program to copy the contents of an object passed into a function. To implement this in C, a function declaration has the following form: [return type] functionName([type][parameter name],...).

Call by Value Example: Swapping the values of the two variables

Output:

We can observe that even when we change the content of x and y in the scope of the swap function, these changes do not reflect on x and y variables defined in the scope of main. This is because we call swap() by value and it will get separate memory for x and y so the changes made in swap() will not reflect in main().

Call by Reference in C

Calling a function by reference will give function parameter the address of original parameter due to which they will point to same memory location and any changes made in the function parameter will also reflect in original parameters. To implement this in C, a function declaration has the following form: [return type] functionName([type]* [parameter name],...).

Call by Reference Example: Swapping the values of the two variables

Output:

We can observe in function parameters instead of using int x,int y we used int *x,int *y and in function call instead of giving x,y, we give &x,&y this methodology is call by reference as we used pointers as function parameter which will get original parameters' address instead of their value. & operator is used to give address of the variables and * is used to access the memory location that pointer is pointing. As the function variable is pointing to the same memory location as the original parameters, the changes made in swap() reflect in main() which we can see in the above output.

When to Use Call by Value and Call by Reference in C?

Copying is expensive, and we have to use our resources wisely. Imagine copying a large object like an array with over a million elements only to enumerate the values inside the array, doing so will result in a waste of time and memory. Time is valuable and we can omit to copy when:

  1. We intend to read state information about an object, or
  2. Allow a function to modify the state of our object.

However, when we do not intend our function to alter the state of our object outside of our function, copying prevents us from making unintentional mistakes and introduce bugs. Now we know when to use call by value and call by reference in C.

Now we will discuss the advantages and disadvantages of call by value and call by reference in C.

Advantages of Using Call by Value Method

  • Guarantees that changes that alter the behavior of a parameter stay within its scope and do not affect the value of an object passed into the function
  • Reduce the chance of introducing subtle bugs which can be difficult to monitor.
  • Passing by value removes the possible side effects of a function which makes your program easier to maintain and reason with.

Advantages of Using Call by Reference Method

  • Calling a function by reference does not incur performance penalties that copying would require. Likewise, it does not duplicate the memory necessary to access the content of an object that resides in our program.
  • Allows a function to update the value of an object that is passed into it.
  • Allows you to pass functions as references through a technique called function pointers which may alter the behavior of a function. Likewise, lambda expressions may also be passed inside a function. Both enable function composition which has neat theoretical properties.

Disadvantages of Using Call by Value Method

  • Incurs performance penalty when copying large objects.
  • Requires to reallocate memory with the same size as the object passed into the function.

Disadvantages of Using Call by Reference Method

  • For every function that shares with the same object, your responsibility of tracking each change also expands.
  • Making sure that the object does not die out abruptly is a serious issue about calling a function by reference. This is especially true in the context of a multithreaded application.
  • A C function has the following form [return_type] [name]([type][parameter_name],...){[body]}.
  • We could specify the mode of passing parameter of function either it is call by value or call by reference.
  • Functions in C can accept functions and lambda expressions.
  • A function can return a pointer, user-defined types (structs), or a primitive data type.

Now we know all important things about call by value and call by reference in C.

Conclusion

  • There are two ways to pass an argument in C: passing by value and passing by reference. Also known as call by value and call by reference in C.

  • Passing by value copies the content of an object that is being passed into the function. This results in an independent object that exists within the scope of that function. This provides a simpler way to think and reason with our program as we do not allow the function to modify the contents of an object.

  • Passing by reference elides copying and instead passes the memory address of an object. A function may be granted with the privileges to modify the values of an object that is passed onto it.

  • Passing by reference enables a function to accept functions and lambda expressions.

  • Managing object references within the program can be difficult to maintain, which may incur a less maintainable codebase.

See Also: