Call by value and Call by reference in C

Function Call by value and Call by reference in C

In this article, I am going to discuss the Function Call by value and Call by reference in C with examples. Please read our previous articles, where we discussed the Types of User-Defined Functions in C

How to call a Function in C Program?

There are two ways that a C function can be called from a program. They are,

  1. Call by value / Pass by Value
  2. Call by reference / Pass by reference
What is Function Call by Value in C?

It is a concept of calling a function by sending value type data or passing the value type data to a function. In call by value, actual arguments and format arguments both are value type data. If any modifications are occurring on formal argument those changes will not be affected by actual arguments.

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments. In call by value method, the value of the variable is passed to the function as parameter. The value of the actual parameter cannot be modified by formal parameter. Different Memory is allocated for both actual and formal parameters. Because value of the actual parameter is copied to the formal parameter.

Note:

  1. Actual parameter – This is the argument that is used in the function call.
  2. Formal parameter – This is the argument which is used in the function definition
Program to understand Call by Value in C:
#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);
int main()
{
    int m = 22, n = 44;
    // calling swap function by value
    printf("values before swap m = %d and n = %d", m, n);
    swap(m, n);
}
void swap(int a, int b)
{ 
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf("\nvalues after swap m = %d and n = %d", a, b);
}

Output:

What is Function Call by Value in C

In this program, the values of the variables “m” and “n” are passed to the function “swap”. These values are copied to formal parameters “a” and “b” in swap function and used.

In the above program swap function is works with the help of call by value mechanism that’s why no modification of swap function can be carried back. In implementation when we are expecting modification then go for the call by address mechanism.

What is Function Call by Reference?

It is a concept of calling the function by sending address type data or passing address type data to a function. In call by address, actual arguments are address type and formal arguments are pointer type. In call by address if any modification occurs on formal arguments those changes will be affected to actual arguments.

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument. In call by reference method, the address of the variable is passed to the function as parameter. The value of the actual parameter can be modified by formal parameter. The same memory is used for both actual and formal parameters since the only address is used by both parameters.

Program to understand Call By Reference in C:
#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b); 
int main()
{
    int m = 22, n = 44;
    //calling swap function by reference
    printf("values before swap m = %d and n = %d",m,n);
    swap(&m, &n); 
}
void swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    printf("\nvalues after swap a = %d and b = %d", *a, *b);
}

Output:

What is Function Call by Reference?

In this program, the address of the variables “m” and “n” are passed to the function “swap”. These values are not copied to formal parameters “a” and “b” in swap function. Because they are just holding the address of those variables. This address is used to access and change the values of the variables.

In the above program swap function will works with the help of call by address mechanism that’s why all modification of swap function carries back to the main function.

Note: C programming language doesn’t support the call by address. Call by address is an OOPL concept that is used to access the data by using the address type. C programming language doesn’t support address type that’s why call by address is not possible.

Difference between call by value and call by reference in c:

Difference between Function call by value and call by reference in c

Return by Value:

When the function is returning value type data then it is called return by value. When the function is not returning any values then specify the return type as void. Void means nothing i.e. function doesn’t return any value. When the function is returning i.e. what type of data it is returning, the same type of return statement required to specify. In the implementation, when the function is returning an integer value, specify the return type as int, i.e. function returning value type called return by value.

Return by Address:

When the function is returning address type data then it is called return by address. When the function is not returning any values then specify the return type as void. When the function is returning the int value then specify the return type as int i.e. function returning value called return by value. When the function is returning an integer value address then specify the return type as an int* i.e. function returning address called return by address. The basic advantage of return by address is one function related to local data can be accessed from outside of the function.

In the next article, I am going to discuss Local vs Global Variables in C with examples. Here, in this article, I try to explain Function Call by value and Call by reference in C. I hope you enjoy this Function Call by value and Call by reference in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article

Leave a Reply

Your email address will not be published. Required fields are marked *