0

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.

  • I think [this post](http://www.cplusplus.com/forum/lounge/62708/) pretty much explains the major difference between call by value and call by reference. In a nutshell, changes to the parameter within the called function (1) *will not change* the actual variable used as the argument to the function if *calling by value*, and (2) *will change* the actual variable used as the argument to the function if *calling by reference*. – wangjunwww Sep 04 '14 at 03:43

4 Answers4

1

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.

Null
  • 1,950
  • 9
  • 30
  • 33
0

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;
}
jazaman
  • 1,007
  • 3
  • 12
  • 30
  • Could you please explain me why you gave int data type as argunent it does not have any variable in line 2 and 3 –  Sep 04 '14 at 03:50
  • 1
    @aVIRA, those lines are function declarations. In function declarations, argument names are not required. They can be there but the declarations are complete without them. – R Sahu Sep 04 '14 at 04:01
0

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).

0

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.

rauprog
  • 243
  • 3
  • 9