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

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.

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

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.

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.

commented: De nada! :-) +12

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.
Where:
- 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.

commented: Please refrain from posting content taken from other sites for your answers. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.