Call By Value And Call By Reference

Call By Value And Call By Reference
Call By Value And Call By Reference

Call By Value And Call By Reference:-

Call By Value:-

In this post we will Learn about Call By Value And Call By Reference in detail and easy manner.

It is a default way for argument passing. Whenever argument is passed by value then a copy of argument is prepared and passed to the formal arguments that is newly created.

The formal argument containa copy of actual argument that is stored in different memory location so any changes made to this are not reflected to the actual argument since changes made in the formal argument are specific to that block which are lost if the control is returned to the calling function.

Example,

//program to show how call by value works?

#include<iostream.h>

#include<conio.h>

void main ()

{

clrscr ();

int a, b;

void swaping (int, int);

cout<<” Enter two number = “;

cin>>a>>b;

cout<<”\n Before calling a = “<<a<<”b = “<<b;

swaping (a, b);

cout<<”\n After calling  a = “<<a<<”b = “<<b;

getch();

}

void swaping (int x, int y)

{

int z;

z= x;

x= y;

y= z;

cout<<”\n After swapping  x = “<<x<<” y = “<<y;

}

Output :-

Enter two number= 10 25

Before calling a= 10 b= 25

After swapping  x = 25 y =10

After calling  a= 10  b= 25

 

Call By Reference:-

It allows the changes back to the actual argument without using pointer variable.It is very easy and simple way of argument passing.

Syntax,

DataType &referencevariablename  = variablename;

Example,

//program to show how pass by reference works?

void main ()

{

clrscr ();

int a, b;

void swaping (int &, int &);

cout<<” Enter two numbers = “;

cin>>a>>b;

cout<<”\n Before calling a= “<<a<<” b = “<<b;

swaping ( a, b);

cout<<”\n After calling a= “<<a<<” b = “<<b;

getch ();

}

void swaping (int &x, int &y)

{

int z;

z= x;

x= y;

y=z;

cout<<”\n After calling  x= “<<x<<” y = “<<y;

}

Output :-

Enter two numbers = 10 25

Before calling a= 10 b= 25

After calling  x= 25 y= 10

After calling  a= 25 b= 10

 

Call By Address (Call By Pointer):-

It is another way of passing argument to a function in which the address of argument is passed to called function.

Whenever any changes are made to the formal argument then all those changes are reflected to the actual argument.

The address of argument is passed by using an address of operator (&) before the name of a variable whose value we want to change.

The formal argument are preceded by asterisk (*) that works as a pointer variable to store the address of the actual argument.

Example,

//program to show how pass by address works?

void  main ()

{

clrscr ();

int a, b;

void swaping (int *, int *);

a= 10; b= 25;

cout<<” Before calling  a= “<<a<<”b = “<<b;

swaping (&a, &b);

cout<<”\n After calling  a= “<<a<<” b = “<<b;

getch ();

}

void swaping (int *x, int *y)

{

int z;

z= *x;

*z= *y;

*y= z;

cout<<” \n After calling  x= “<<*x<<” y = “<<*y;

}

Output :-

Before calling  a= 10 b= 25

After calling  x= 25 y= 10

After calling   a= 25 b= 10

 

A way to transferring data between the calling function and the called function is known as Argument Passing Techniques.  Above all are the same Techniques.

You Also Can Visit For More Knowledge In C++:-

Visit Your YouTube  Channel:-  Think , About It

Visit Your Website :- www.atozlives.com

Thanks,

“Play With All Computer Science , Mathematics  And Technology.”

 

 

Previous articleInline Function- Program And Its Uses
SHARE

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here