Call by value and by reference in function
Functions in C++
Hello! Everyone in previous blog
we learned about function and in today’s blog we will continue previous blog
that means we will learn about use of function using call by value and call by
reference.
What is call by value?
basically what we do when we awoke
a function we provide value or argument to process with, in the same way we can
provide these value to function by these two method.
1.Call by value method
2.Call by reference method
1.Call by value method
In the call by value method,
the original value can not be changed. When you pass any argument to function,
it is stored locally by the function parameter in memory called stack memory. Hence,
the values are changed inside the function only and it will not have an effect
outside the function.
Here I am giving you an example of swapping two
number by call by value by which you can understand this concept better.
Example-
#include <iostream>
using namespace std;
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
cout<<"\n"<<"value of a inside the function:
"<<a;
cout<<"\n"<<"value of b inside the function:
"<<b;
}
int main()
{
int a = 50, b = 70;
cout<<"value of a before sending
to function: "<<a;
cout<<"\n"<<"value of b before sending to
function: "<<b;
swap(a, b);
// passing value to function
cout<<"\n"<<"value of a after sending to
function: "<<a;
cout<<"\n"<<"value of b after sending to
function: "<<b;
return 0;
}
Output will be like this-
value of a before sending to
function: 50
value of b before sending to
function: 70
value of a inside the
function: 70
value of b inside the
function: 50
value of a after sending to
function: 50
value of b after sending to
function: 70
2. Call by reference
In the call by reference, the original value is changed because we pass
reference means address of argument in place of original arguments. The actual
and formal arguments share the same address space, so any changes of value
inside the function reflected inside as well as outside the function.
Here we will see same example of
swapping but thorough call by reference.
Example-
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
cout<<"\n"<<"value of a inside the function:
"<<*a;
cout<<"\n"<<"value of b inside the function:
"<<*b;
}
int main()
{
int a = 50, b = 75;
cout<<"\n"<<"value of a before sending to
function: "<<a;
cout<<"\n"<<"value of b before sending to
function: "<<b;
swap(&a, &b); // passing value to function
cout<<"\n"<<"value of a after sending to
function: "<<a;
cout<<"\n"<<"value of b after sending to
function: "<<b;
return 0;
}
Output will look like-
value of a before sending to
function: 50
value of b before sending to
function: 75
value of a inside the
function: 75
value of b inside the
function: 50
value of a after sending to
function: 75
value of b after sending function: 50
Thank you for reading this in
the next blog we will learn about recursive function and recursion in C++
Read more about my previousblog function in C++ click here………

You work very hard my best luck with you 🤲👍💖
ReplyDeleteGood work👍 keep it up
ReplyDelete