Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ Call by reference VS Call by value

Posted on 2009-02-22
4
Medium Priority
?
2,776 Views
Last Modified: 2013-12-14
I don't really think that I understand the difference between calling by value and calling by reference.

When I call by value, the function doesn't modify variables in the class, but it only creates local variables inside the function?  And when I call by reference, the function actually modifies the variables?

I'm confused.  I've looked online, but I just don't get it.  Can anybody give me as simple Barney style explanation?
0
Comment
Question by:ubuntuguy
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 23707126
>>When I call by value, the function doesn't modify variables in the class, but it
>>only creates local variables inside the function?

To some extent, but classes are not of relevance here - a call by value creates a copy of the argument that then is passed to the function, so any modification of the parameter is "invisible" and has no effect to the data outside of that function. If you do a "call by reference", you don't get a copy of the parameter, but the actual instance, so you can (but don't have to) manipulate the original data.

A simple example would be
#include <iostream>
using namespace std;
 
void by_value(int n) {
 
  n += 42;
 
  cout << n << endl;
 
}
 
void by_reference(int& n) {
 
  n += 42;
 
  cout << n << endl;
 
}
 
int main () {
 
  int n = 42;
 
  cout << "Calling 'by value'" << endl;
 
  by_value(n);
 
  cout << "'n' now is " << n << endl;
 
  cout << "Calling 'by reference'" << endl;
 
  by_reference(n);
 
  cout << "'n' now is " << n << endl;
 
  return 0;
}
 
// output:
 
Calling 'by value'
84
'n' now is 42
Calling 'by reference'
84
'n' now is 84

Open in new window

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23707184
ok I think I I get it... so basically when I call by reference the parameter in the function has to be a variable that already exits, and if its value changes inside the function then its original value is changed too outside the function.  if I call by value then whatever change happens to the parameter inside the function has to effect on its value after the function has been called, the value of the parameter remains the same.

0
 
LVL 86

Expert Comment

by:jkr
ID: 23707242
Yes, that pretty much sums it up. Now, if you thing the above sample to pass objects instead of integers, you have the big picture.
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23707501
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question