Call by Value and Call by reference, C Language Assignment Help

Assignment Help: >> Functions >> Call by Value and Call by reference, C Language

Call by Value and Call by reference

 There are two ways in which we can pass arguments to the function.

1.       Call by value

2.       Call by reference

Call by value: This value of actual argument are passed to the formal arguments & the operation is done on the formal arguments. and any change made in the formal argument does not affect the actual arguments becoz formal arguments are photocopy of actual arguments. Hence when function is called by the call or by value method, it does not affect the actual contents of the actual arguments. Changes made in the formal arguments are local to the block of the called function.

Example: Write a program to send values by call by value.

#include<stdio.h>

#include<conio.h>

main()

{

 int a,b,change(int,int);

clrscr();

printf("\n Enter Values of A and B:");

scanf("%d%d",&a,&b);

change(a,b);

printf("\n In main() A=%d B=%d",a,b);

}

change(int x, int y)

{

 int t;

t=a,a=b,b=t;

printf("\n Change( ) A=%d B=%d",x,y);

}

Call by reference: In this type instead of passing values, addresses (reference) are passed. Function operates on addresses rather than values. Here the formal arguments are pointer to the actual argument. In this type formal arguments point to the actual argument. Hence changes made in the arguments are permanent.

Example: Write a program to send a value by reference to the user-defined function.

#include<stdio.h>

#include<conio.h>

main()

{

                int a,b,change(int*,int*);

clrscr();

printf("\n Enter Values of A and B:");

scanf("%d%d",&x,&y);

change(&x,&y);

printf("\n In main() A=%d B=%d",x,y);

}

change(int *a, int *b)

{

int *t;

*t= *a, *a= *b, *b= *t;

printf("\n In change() A=%d B=%d",*a, *b);

}

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd