Reputation:
Following code is an example to call function in both methods Please tell me major difference or meaning between call by value and call by reference 1.Call by value. 2.Call by reference. The following code illustrates call by value method.
I specified my doubts in comments
#include<iostream>
int main(){
void change(int);//why function prototype is before function definition and what is
int orig=10;//meaning of argument int, it did not defined any variable of type int
cout<<"The original value is: "<<orig<<"\n";
change(orig);//what is the meaning of this piece of code
cout<<"Value after change() is over:"<<orig<<"\n";
return 0;
};
void change(int orig){
orig=20;
cout<<"Value of orig in function change() is:"<<orig<<"\n";
return;
}
In book i read that function definition should before function prototype.
Upvotes: 0
Views: 2785
Reputation: 243
A good example of the difference between passing by reference or passing by value is when an array is passed to a function. It can be passed by pointer or by reference. A good example is at the link here.
Upvotes: 0
Reputation: 56
Pass by value mean that if you change value inside function then it will have no affect outside of it (when function returns). Where as pass by reference means that if value is changed in function it will also be changed outside (when function returns).
Upvotes: 0
Reputation: 1970
Call by value makes a copy of the argument and puts it in a local variable for use by the function, so if the function changes the value of the local variable the argument itself is not changed. Call by reference passes a reference of the argument to the function rather than a copy, so if the function changes the value of the argument then the argument itself is changed.
The function prototype void change(int);
tells the compiler that there is a function named change which takes a single argument of type int
and returns void
(i.e. nothing). It is call by value since there is no &
with the argument. Later in your code you have the line change(orig);
which actually calls the function with argument orig
of type int
. Since the function prototype was declared before this function call the compiler recognizes it as a function.
Take a look at the output of this program:
#include<iostream>
using namespace std;
int main(){
void change(int);
void change2(int&);
int x = 10;
cout<<"The original value of x is: "<< x <<"\n";
change(x); // call change(), which uses call by value
cout<<"Value of x after change() is over: "<< x <<"\n";
change2(x); // call change2(), which uses call by reference
cout<<"Value of x after change2() is over: "<< x <<"\n";
return 0;
};
void change(int orig){
cout<<"Value of orig in function change() at beginning is: "<<orig<<"\n";
orig=20;
cout<<"Value of orig in function change() at end is: "<<orig<<"\n";
return;
}
void change2(int &orig){
cout<<"Value of orig in function change2() at beginning is: "<<orig<<"\n";
orig=20;
cout<<"Value of orig in function change2() at end is: "<<orig<<"\n";
return;
}
I've changed int orig
in main()
to int x
to hopefully avoid name confusion, and I've added change2()
which uses call by reference.
Upvotes: 1
Reputation: 1019
The main difference is "pass by reference" pass the reference of the object not a copy of the object. So the called function may access the same memory location where the object is located and modify it if requires. On the contrary, when a object's copy is passed the modification is made to the copied object not to the original one.
You may add the following in your code and see the difference:
#include<iostream>
void change(int);
void change_ref(int&);
int main(){
int orig=10;//meaning of argument int, it did not defined any variable of type int
cout<<"The original value is: "<<orig<<"\n";
change_ref(orig);//what is the meaning of this piece of code
cout<<"Value after change() is over:"<<orig<<"\n";
return 0;
};
void change_ref(int& orig){
orig=20;
cout<<"Value of orig in function change() is:"<<orig<<"\n";
return;
}
Upvotes: 0