In this article call by value and call by reference we give the information about In this, the values of the parameters of the calling function are copied to the variables declared in the parameters of the function-definition.
call by value and call by reference
Passing arguments to function in C++:-
There are 3 way to Passing arguments to functions such as follows:-
Passing arguments to function:
1. Call by value in C++
In this, the values of the parameters of the calling function are copied to the variables declared in the parameters of the function-definition i.e. the values of the actual parameter are copied to the formal parameter.
Function definition only uses the values of the actual parameters copied by the formal parameters. In a way, there are duplicate values in the function definition. Therefore, even if the declared variable -name in both the parameters are same, there is no change in the value of the actual parameter when the values of the formal parameter are changed.
#include<iostream.h>
#include<conio.h>
void value(int,int); // function declaration
void main()
{
clrscr();
int x,y; // variable declaration
cout<<“\n Enter two number: “;
cin>>x>>y;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
value(x,y); // call by value
cout<<“\n After call by value”;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
getch();
}
void value (int x, int y) // function definition
{
int z;
z = x;
x = y;
y = z;
}
OUTPUT
Enter two numbers: 12
13
First number: 12
Second number: 13
After call by value
First number: 13
Second number: 12
Call by reference in C++:-
In this, there are reference variables in the formal parameter, which are references to the actual parameter which has a different variable name. Here, just like call by value, the values of the actual parameter are copied into the formal parameter. But when the values of the formal parameter change, the value of the actual parameter also changes.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y; // variable declaration
void value(int&,int&); // function declaration
cout<<“\n Enter two number: “;
cin>>x>>y;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
value(x,y); // call by reference
cout<<“\n After call by reference”;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
getch();
}
void value (int &a, int &b) // function definition
{
int c;
c = a;
a = b;
b = c;
}
OUTPUT
Enter two number: 12
13
First number :1 2
Second number: 13
After call by reference
First number : 13
Second number: 12
3. Call by pointer in C++:-
This method of parameter passing is different from the other two. To understand this, it is necessary to understand pointer. In this method, the memory address of the values is passed in the function definition, As we know that we can access the memory address of any values using a pointer variable. Therefore, the function definition consists of formal parameters, pointer variables, which access the address of the actual parameter.
In this way the memory address pass of the values in the function definition is, In this also, when the value of the formal parameter is changed, as in call by reference, the value of the actual parameter also changes.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y; // variable declaration
void value(int*,int*); // function declaration
cout<<“\n Enter two number: “;
cin>>x>>y;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
value(&x,&y); // call by pointer
cout<<“\n After call by pointer”;
cout<<“\n First number : “<<x;
cout<<“\n Second number: “<<y;
getch();
}
void value (int *a, int *b) // function definition
{
int c;
c = *a;
*a = *b;
*b = c;
}
OUTPUT
Enter two number: 12
13
First number : 12
Second number: 13
After call by pointer
First number : 13
Second number: 12
In this way we pass the values in the function definition by passing a parameter.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining