Hi Brethrem,
am kind of new in the C++ world, so far am learning about functions but am stuck on the difference between call a function by reference and call it by value.
I will appreciate your help

Edited by GOMEZ_1

2 Years
Discussion Span
Last Post by melissad

I think you mean passing variables by reference or by value when you call the function. The difference is basically what effect you can have on the variable.

By value means that the variable passed into the function is a copy of the original variable and any changes made to the copy won't affect the original.

By reference means that the address of the original variable is passed to the function, and any changes will be made to the original.

Edited by tinstaafl


@tinstaafl thanks for that. I really meant that. Then passing by reference should be a good choice when it comes to function calling, right? or it depends on the program you are coding?


@tinstaafl thanks for that. I really meant that. Then passing by reference should be a good choice when it comes to function calling, right? or it depends on the program you are coding?


Whether to pass parameters by reference or value largely depends on the type of data you're passing to a function.

Passing by reference is more efficient for complex types. Instead of copying the whole object, you need only pass a reference or pointer to the object, which is typically 32 or 64-bits in size depending on your system.

Simple datatypes like signed and unsigned integers, floats and bools, are no larger than a pointer, so there's no advantage to passing these by reference. In fact passing simple datatypes by pointer might actually be slower because the value will need to be dereferenced.

Edited by LaxLoafer


There are several issues here. There are references, const references, pointers, and const pointers. In C++ references of either type are preferred to pointers. Why? Because the function doesn't need to verify that the pointer is null before accessing it. If passed as a reference, the object has to be instantiated (not null), otherwise the compiler will complain.

If a reference is const, then you cannot modify it. If it is not const, then the function can modify the object, and the source object will be changed (they are the same). BTW, for OOP (including C++), functions are static or global. If the function is a class member, then it is a method. They are very different.

FWIW, passing data as a pointer or reference is pretty much equally efficient. As tinstaafl pointed out, passing objects directly can be as efficient, provided they are the size of pointers, or smaller. So, on modern systems where pointers are 64 bits, you can pass any data that is 8 bytes or less with the same overhead; however, changing an object passed directly as such, you get a copy (there is some small performance penalty in this), so changing it will not affect the passed object. Caveat User! :-)

In the end, I prefer to pass data as references (const or non-const as necessary). The const/non-const designation is to indicate my intention whether the called function/method is allowed to change the state of the object.

Edited by rubberman


Alright, thanks LaxLoafer and @ Rubberman I get the concept, actually I didn't also had the idea of const and non-const references. And also now I get it that an object passed as a reference should never be null though a pointer can...thanks a lot, this is helping me to increase my studying pace...More Grace.

Votes + Comments
De nada! :-)

Functions allow to structure programs in segments of code to perform individual tasks.

In C++, a function is a group of statements that is given a name, and which can be called from some point of the program.
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
- statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

Votes + Comments
Please refrain from posting content taken from other sites for your answers.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.