Call by Reference

Call by reference is the method in C where we call the function with the passing address as arguments. We pass the address of the memory blocks which can be further stored in a pointer variable that can be used in the function. Now, changes performed in the values inside the function can be directly reflected in the main memory.

Example:

C




// C Program to implement
// Call by reference
#include <stdio.h>
  
// Call by reference
void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
  
// Driver Code
int main()
{
    // Declaring Integer
    int x = 1, y = 5;
    printf("Before Swapping: x:%d , y:%d\n", x, y);
  
    // Calling the function
    swap(&x, &y);
    printf("After Swapping: x:%d , y:%d\n", x, y);
  
    return 0;
}


Output

Before Swapping: x:1 , y:5
After Swapping: x:5 , y:1

C Function Arguments and Function Return Values

Prerequisite: Functions in C

A function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values. Hence the function prototype of a function in C is as below: 

Similar Reads

Call by Value

Call by value in C is where in the arguments we pass value and that value can be used in function for performing the operation. Values passed in the function are stored in temporary memory so the changes performed in the function don’t affect the actual value of the variable passed....

Call by Reference

...

Types of Function According to Arguments and Return Value

Call by reference is the method in C where we call the function with the passing address as arguments. We pass the address of the memory blocks which can be further stored in a pointer variable that can be used in the function. Now, changes performed in the values inside the function can be directly reflected in the main memory....

Contact Us