Types of Switching Techniques

Call by Value and Call by Reference

Sep 17 • General, Notes • 53341 Views • 1 Comment on Call by Value and Call by Reference

Register Your Coaching

oureducation.in Register Your Coaching

register your self as teacher

oureducation.in register your self as teacher

Introduction

In C programming language, we have different parameters passing schemes. Here we are going to discuss about the two most important ways of passing arguments to a functions such as call by value and call by reference.

Important arguments passing techniques in C

    • Call by Value
    • Call by Reference
C

Passing Arguments in C

Call by Value : If we call a function in C  by passing values of variables as the parameters/arguments to the function then such type of function call is known as Call by value.
Call by Reference : We know that whatever variable we are using in our program, ultimately it is getting stored somewhere in memory. So instead of passing values of variables as parameters to the function, if we pass address of those variables and somehow able to access data contained inside those addresses then we will be able to call functions. Interestingly, concepts of Pointers provide us the advantage of manipulating addresses.So calling a function by passing addresses of variables as the parameters to the function is known as Call by Reference.

you can join our whats app group +918800222298 or call 01204221413 or mail@oureducation.in for updates on Jobs or interviews or current GD topics

Concept of Actual and Formal arguments

Arguments passed to the function during function call are known as actual arguments and the arguments we use during a function definition are known as formal arguments. For a function call to be valid the type, order and number of actual and formal arguments must always be same.

During call by value method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In this method, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.

GATE Syllabus details

You should also know about AMCAT

Program Code to demonstrate call by value method

#include<stdio.h>
int sum(int x, int y);   /*function declaration*/
int main()
{
int num1, num2, add;
printf(” Enter any two numbers \n”);
scanf(“%d, %d”, &num1, &num2);
add = sum(num1, num2);  /*Calling the add function */
return 0;
}

int add( int x,  int y)    /* Function Definitions */
{
int sum;
sum=x+y;
return sum;
}

During call by reference, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. In this method we can alter the actual arguments by doing some changes to formal arguments.

Program code to demonstrate call by reference method

#include<stdio.h>
void swap(int *, int *);  /*function declarations */
int main();
{
 int a=5, b=6;
swap(&a, &b);                   /*Calling functions by reference //passing the address */
printf(“a=%d b=%d \n”,a,b);
return 0;
}

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

For questions related to functions and function calls visit the link below

1. Interview-Question-and-Answers-on-C.pdf
2. Interview Questions on C++ 
3. Interview Questions on C 

Please give your valuable feedback through the comments. Visit again.

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

« »