×

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is given an alias for the argument passed, and any changes made to the parameter inside the function will affect the argument used in the call. In contrast, call by value means that the argument is copied into the parameter of the called function, and any changes made to the parameter within the function do not affect the argument used in the call.

When a function is called by reference, the memory address of the argument is passed to the function. However, when a function is called by value, a copy of the argument is passed to the function. This difference makes a significant impact on the way a program behaves. For example, if a function is called by reference and the parameter is modified within the function, the changes will be reflected outside the function. On the other hand, if the same function is called by value and the parameter is modified, the changes will not be reflected outside the function.

When we declare a function in the C programming language, we can specify whether the parameters should be passed by reference or by value.

The syntax for declaring a function with reference parameters is as follows:

Type funcName(Type &var1, Type &var2, ...);

The syntax for declaring a function with value parameters is as follows:

Type funcName(Type var1, Type var2, ...);

For example, consider the following function that multiplies two numbers:

// Function declared with reference parameters 
int Multiply(int &a, int &b)
Call by Value and Call by Reference

Call by Value

In the C programming language, "call by value" is the method of passing arguments to a function in which the function receives a copy of the value of the argument, rather than the original variable. This means that any changes made to the argument within the function have no effect on the original variable outside of the function. In C, all basic data types (e.g. int, float, char) are passed by value by default.

Here's an example of call by value in C:

Example:

#include <stdio.h>


void swap(int x, int y) {
    int temp = x;
    x = y;
    y = temp;
}


int main() {
    int a = 5, b = 10;
    printf("Before the swap: a = %d, b = %d\n", a, b);
    swap(a, b);
    printf("After the swap: a = %d, b = %d\n", a, b);
    return 0;
}

Output:

Before the swap: a = 5, b = 10
After the swap: a = 5, b = 10

In this example, the function swap takes two integer arguments, x and y, and swaps their values using a temporary variable temp. When we call swap(a, b) in the main function, the values of a and b are copied into x and y, respectively. The swap happens within the function but the original values passed to the swap() aren't changed.

The output of the code will be:

Before the swap: a = 5, b = 10

After the swap: a = 5, b = 10

As the variables passed to the swap are copies of the original variables. So, any change made on the copies doesn't affect the actual variables.

Call by Reference

In C, "call by reference" is the method of passing arguments to a function in which the function receives a reference (or pointer) to the original variable, rather than a copy of its value. This means that any changes made to the argument within the function are reflected in the original variable outside of the function.

To pass an argument by reference, you need to pass the address of the variable using the '&' operator which is called the address of the operator.

Here's an example that demonstrates how call by reference works in C:

Example:

#include <stdio.h>


void swap(int* x, int* y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}


int main() {
    int a = 5, b = 10;
    printf("Before the swap: a = %d, b = %d\n", a, b);
    swap(&a, &b);
    printf("After the swap: a = %d, b = %d\n", a, b);
    return 0;
}

Output:

Before the swap: a = 5, b = 10
After the swap: a = 10, b = 5

In this example, the swap function takes two arguments, which are pointers to integers (int*). The & operator is used in the main function to pass the addresses of a and b as arguments to the swap function. Inside the function, the * operator is used to dereference the pointers and access the values of the variables. This way, x and y point to the original variables a and b respectively, and any changes made to them affect the original variables. So, the output will be:

Before the swap: a = 5, b = 10

After the swap: a = 10, b = 5

In C language, all pointer types are passed by reference by default.


Related Topics

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.