51364

functions in c++ call by value and call by reference

Question:

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 <strong>1.Call by value. 2.Call by reference.</strong> 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.

Answer1:

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.

Answer2:

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; }

Answer3:

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

Answer4:

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 <a href="http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm" rel="nofollow">here.</a>

Recommend

  • gvim Windows - Message popup box with OK button?
  • Printing addresses of Perl object methods? (for redefining Perl class methods)
  • Concatenate two audio blobs JavaScript
  • how to use javascript replace all of it? [closed]
  • pointer substrings arrays
  • Writing data to memory in C++
  • LC3 Assembly Square of N
  • Go - modify dereferenced struct pointer changes most struct values, but not slices
  • Java - Convert Big-Endian to Little-Endian
  • How to get smooth mouse events for a canvas “drawing style” app?
  • Getting data from matplotlib axes object
  • Making header act like google chrome mobile app address bar
  • Salesforce Different WSDL files and when to use
  • Who propagate bugfixes across branches (corporate development)?
  • xcode don't localize specific strings
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Why does access(2) check for real and not effective UID?
  • ilmerge with a PFX file
  • Illegal mix of collations for operation for date/time comparison
  • How can I use Kendo UI with Razor?
  • Build own AppleScript numerical error handling
  • Weird JavaScript statement, what does it mean?
  • Websockets service method fails during R startup
  • How do you troubleshoot character encoding problems?
  • R: gsub and capture
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • Transpose CSV data with awk (pivot transformation)
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • Net Present Value in Excel for Grouped Recurring CF