×

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

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 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.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

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.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.