Functions could someone explain...

closed account (zybCM4Gy)
On the flip side, functions with reference parameters are generally perceived as functions that modify the arguments passed, because that is why reference parameters are actually for.


http://www.cplusplus.com/doc/tutorial/functions/

Right. I admit it, I'm as thick as two short planks. However I have no idea what on earth this sentence means.

Of course the real point is, I don't understand why we should be using constant references instead of just passing the damn thing by value in the first place.

Could someone provide an example? (Or rather another example demonstrating in what instance it would be better to use a constant reference rather than pass by value or am I just misunderstanding the whole thing)?
If they objects are expensive to copy you should pass by reference. The built-in (primitive) types[int,char,ect..] are pretty cheap to pass. Some people will even say to just pass everything by const reference if you are not going to be modifying.

Here is another link that has more information.
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
Pass by const reference

One of the major disadvantages of pass by value is that all arguments passed by value are copied to the parameters. When the arguments are large structs or classes, this can take a lot of time. References provide a way to avoid this penalty. When an argument is passed by reference, a reference is created to the actual argument (which takes minimal time) and no copying of values takes place. This allows us to pass large structs and classes with a minimum performance penalty.

However, this also opens us up to potential trouble. References allow the function to change the value of the argument, which in many cases is undesirable. If we know that a function should not change the value of an argument, but don’t want to pass by value, the best solution is to pass by const reference.

You already know that a const reference is a reference that does not allow the variable being referenced to be changed. Consequently, if we use a const reference as a parameter, we guarantee to the caller that the function will not (and can not) change the argument!

The following function will produce a compiler error:

1
2
3
4
void foo(const int &x)
{
    x = 6;  // x is a const reference and can not be changed!
}


Using const is useful for several reasons:

It enlists the compilers help in ensuring values that shouldn’t be changed aren’t changed.
It tells the coder whether they need to worry about the function changing the value of the argument
It helps the coder debug incorrect values by telling the coder whether the function might be at fault or not
Rule: Always pass by const reference unless you need to change the value of the argument

Summary

Advantages of passing by reference:

It allows us to have the function change the value of the argument, which is sometimes useful.
Because a copy of the argument is not made, it is fast, even when used with large structs or classes.
We can pass by const reference to avoid unintentional changes.
We can return multiple values from a function.

Disadvantages of passing by reference:

Because a non-const reference can not be made to a literal or an expression, reference arguments must be normal variables.
It can be hard to tell whether a parameter passed by reference is meant to be input, output, or both.
It’s impossible to tell from the function call that the argument may change. An argument passed by value and passed by reference looks the same. We can only tell whether an argument is passed by value or reference by looking at the function declaration. This can lead to situations where the programmer does not realize a function will change the value of the argument.
Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value.
closed account (zybCM4Gy)
Thanks!
Topic archived. No new replies allowed.