In this article call by reference in c When we pass the address of a variable to that function while calling a function it is called call by reference or call by address.

Call by Value and Call by Reference

Call By Value in C:

As we know, no function comes into the program without calling and is not execute. If we need to use any type of function in our program, then first we have to call it.

If we need to call function then first, we must to writing the name of the function then it is called function calling.

While calling the function, if any argument or value is to be passed to that function then we pass that argument or value by writing it in parenthesis () while calling the function.

In c language we can pass two types of values in parenthesis of function then two types of calling function such as follows:

  1. Call by Value
  2. Call by Reference in C

Before telling about call by value and call by reference, I want to tell you what difference between the actual parameters and formal parameters are.

Actual parameters:

While calling a function when we write some value or argument in its parenthesis it is called actual parameters.

Formal parameters:

While defining the function, when we write or define some argument in parenthesis it is called formal parameters or arguments.

Call By Value in C:

We need to call the function then you pass a value or the value of a variable by writing in it the parenthesis of the function then it is called call by value.

In this the value of the actual parameters is passed or assigned to the formal parameters so that the function can use the value to complete its task.

There is no effect in the original value in call by value because both the values are stored in different locations.

Program:

// Perimeter of rectangle

#include<stdio.h>

#include<conio.h>

int perimeters (int l, int b);                   // function declaration

void main ()

{

            int n1=5 , n2=10;

            int n3=perimeter (n1, n2);                        // function call

            printf(“perimeter of rectangle is:%d”,n3);

}

int perimeter (int l, int b)                                     // function definition

{

             int p=2(l+b);

            return p;

}

O/P:- perimeter of rectangle is : 30.

Advantages of call by value in C:

  • It does not change the value of the original variable, which keeps the data secure
  • When the function is called it has no effect in the arguments of the actual parameter
  • In this the value of the actual parameter goes to the formal parameter, so that if any change is made then its effect is only on the formal parameter, it does not create any problem on the actual parameter.

Disadvantages of call by value in C:

  • There is no permission to you cannot change the value of the original variable inside the function
  • The actual argument all ways be a variable
  • Same value is stored in two different variable which is not memory efficient

Use of call by value:

  • When we do not want to change the original value of the variable then we use call by value.
  • We use it when we do not want any kind of side effect in the value of the actual parameter.
  • When we have to pass only value to the function to do it’s work then we use call by value.

Call by reference in C:

When we pass the address of a variable to that function while calling a function it is called call by reference or call by address.

In this the address of any variable in the actual parameter is passed to the pointer variable (which can store the address of any variable) kept in the formal parameters. By doing this the function can also change the value kept in that address changes also happen in the original value in call by reference because both the values are stored in the same location. In this, the address of the variable kept in the actual parameters is stored in the pointer variable kept in the formal parameters.

Program:

// swapping the values of the two variables

#include<stdio.h>

#include<conio.h>

void swap (int *p, int *q);                                   // function declaration

void main ()

{

            int n1=50;

            int n2=60;

            printf(“Before swap value of n1%d\n”,n1);

            printf(“Before swap value of n2%d\n”,n2);

            swap (&n1,&n2);                                                         // function call

            printf(“After swap value of n1%d\n”,n1);

            printf(“After swap value of n2%d\n”,n2);

}

void swap (int*p, int*q)                                        // function definition

{

            int temp;

            temp=*p;

            *p=*q;

            *p=temp;

}

O/P:

Before swap value of x:50

Before swap value of y:60

After swap value of x:60

After swap value of y:50

Advantages of call by reference in C:

  • It does not have to create duplicate variable to store the same value.
  • It does not cause memory loss.
  • In this we can change the value of the original variable inside the function.
  • By passing the address we are able to change the value of that variable.

Disadvantages of call by reference in C:

  • Variable changes inside the function affect the original variable.
  • Call by reference can sometimes cause problems in the program and the program can be complex.

Use of call by reference:

  • When we want to make any change in the original value of the variable then we use call by reference.
  • We want to change the value of the actual parameter then we use it.
  • When we have to pass a reference to that variable for the function to do it’s work then we use call by reference.

Difference between call by value and call by reference.

Call by value

Call by reference

  • The copy of the value of the variable is passed to the function.
  • The address of the variable is passed to the function.
  • The change in value inside in a function is limited to that function only. Change to the formal parameter do not affect the actual parameter.
  • The change in value inside a function is not limited to that function. A change in a formal parameter affect the actual parameter.
  • Actual and formal arguments are located in the different locations in the memory so that change is one do not affect the other
  • Actual and formal arguments are located in the same location in one affect the other.
  • There is no change in the original value.
  • The original value is also changes.
  • A normal variable is used to store the value passed to the function.
  • A pointer variable is needed to store the address of the variable passed to the function.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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