Header Ads

Call by value and Call by reference

Call by Value and Call by Reference


Now we know to call Functions. But till now when we call the Function either by passing value or simple function call always by passing the values. If we call the function by passing its value then such a function call or called is known as call by value or when we pass the value of variable for function call such a function call is known as call by value.
Below is an example of call by value:


#include<stdio.h>
#include<conio.h>
int factorial(int);
main()
{
int num,fact;
printf("Enter the number: ");
scanf("%d",&num);
fact=factorial(num);
printf("FACTROIAL of %d = %d",num,fact);
getch();
}
int factorial(int n)
{
int i=1,f=1;
while(i<=n)
{
f*=i;
i++;
}
return (f);
}

Let this time you sort out the output for this program….
While when we pass the address of a variable and able to access the value at that address. Then, such function calls are known as call by reference. This feature of C needs at least one additional knowledge of ‘Pointers’. 

POINTER


Consider the declaration,
Int i=3;
This declaration tell the C compiler to:
·        Reserve space in memory to hold the integer value.
·        Associate the name i with this memory location.
·        Store the value 3 at this location.
We can print this address number through following program:


#include<stdio.h>
int main()
{
    int i=3;
    printf("Address  of i = %u\n",&i);
    printf("Value of i = %d\n",i);
   printf(“Value of i = %d\n”,*(&i));
    return 0;
}

Output 
Address of i = 2686748
Value of i = 3
Value of i = 3_
Hence it is printed out using %u which is a format specifier for printing an unsigned integer.
Note printing the value of *(&i) is same as printing the value of i.
The expression &i gives the address of the variable i. This address can be collected in a variable, by saying,
j=&i
But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of another variable (i in this case ).
i Contains the value 3 whose address is 2686748 and j’s value is i’s address i.e. 2686748.
Let see a program for better clearance…..

#include<stdio.h>
int main()
{
    int i=3;
    int *j;
    j=&i;
    printf("Address  of i = %u\n",&i);
    printf("Address  of i = %u\n",j);
    printf("Address  of j = %u\n",&j);
    printf("Value of j = %d\n",j);
    printf("Value of i = %d\n",i);
    printf("Value of i = %d\n",*(&i));
    printf("Value of i = %d\n",j);
    return 0;
}

Output

Address of i = 2686748
Address of i = 2686748
Address of j = 2686744
Value of j = 2686748
Value of i = 3
Value of i = 3
Value of i = 3
_

Carefully observe the above program for better understanding.

Back to function call by reference
Following program illustrates the fact


#include<stdio.h>
int swap(int *,int *);
int main()
{
    int a=10,b=20;
    swap(&a,&b);
    printf("a = %d b = %d\n",a,b);
    return 0;
}
int swap(int *x,int *y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}

Output
a =20 b = 10
_

Here is another program show how to return more than one value at a time

#include<stdio.h>
int areaper(int,float *,float *);
int main()
{
    int rad;
    float area,per;
    printf("Enter the radius of circle ");
    scanf("%d",&rad);
    areaper(rad,&area,&per);
    printf("Area = %f\n",area);
    printf("Perimeter = %f\n",per);
    return 0;
}
int areaper(int r,float *a,float *p)
{
    *a=3.14*r*r;
    *p=2*3.14*r;
}

Output

Enter the radius of circle 3
Area = 28.26000
Perimeter = 18.840000

_

No comments