Skip to main content

Call by Value and Call by Reference

There are two ways to pass arguments/parameters to function calls -- call by value and call by reference.. The major difference between call by value and call by reference is that in call by value a copy of actual arguments is passed to respective formal arguments. While, in call by reference the location (address) of actual arguments is passed to formal arguments, hence any change made to formal arguments will also reflect in actual arguments.
In C, all function arguments are passed "by value". The calling and called functions do not share any memory -- they have their own copy and the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy. There are cases we need call by reference.


1.      The called function communicates to the calling function only through return statement and return statement can only send one value back to the calling function. If there are more than one value we want to alter, call by reference is required
2.     If the size of data is large , copying actual arguments to formal arguments could be a time consuming operation and occupies more memory.

To achieve call by reference functionality in C language the calling function provides the address of the variable to be set (technically a pointer to the variable), and the called function declares the parameter to be a pointer and access the variable indirectly through it. Since the address of the argument is passed to the function, code within the called function can change the value of the actual arguments.
call by value
call by reference
In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.
In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.
In call by value, actual arguments will remain safe, they cannot be modified accidentally.
In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.


Example using Call by Value
The classic example of wanting to modify the caller's memory is a swapByValue() function which exchanges two values. For C uses call by value, the following version of swap swapByValue() will not work...
#include <stdio.h>
void swapByValue(int, int); /* Prototype */
 int main() /* Main function */
{
  int n1 = 10, n2 = 20;
   /* actual arguments will be as it is */
  swapByValue(n1, n2);
  printf("n1: %d, n2: %d\n", n1, n2);
}
 void swapByValue(int a, int b)
{
  int t;
  t = a; a = b; b = t;
}
 OUTPUT
======
n1: 10, n2: 20
The swapByValue() does not affect the arguments n1 and n2 in the calling function it only operates on a and b local to swapByValue() itself. This is a good example of how local variables behave.

Example using Call by Reference

 

In call by reference, to pass a variable n as a reference parameter, the programmer must pass a pointer to n instead of n itself. The formal parameter will be a pointer to the value of interest. The calling function will need to use & to compute the pointer of actual parameter. The called function will need to dereference the pointer with *where appropriate to access the value of interest. Here is an example of a correct swap swapByReference() function. So, now you got the difference between call by value and call by reference!
#include <stdio.h>
void swapByReference(int*, int*); /* Prototype */
 int main() /* Main function */
{
  int n1 = 10, n2 = 20;
   /* actual arguments will be altered */
  swapByReference(&n1, &n2);
  printf("n1: %d, n2: %d\n", n1, n2);
}
 void swapByReference(int *a, int *b)
{
  int t;
  t = *a; *a = *b; *b = t;
}
 OUTPUT
======
n1: 20, n2: 10


Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …