Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?

Not much to say other than:

Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?

I mean either using:

1
2
3
4
void ptr_Func(int *x)
{
    *x = *x+1
}


or

1
2
3
4
int ptr_Func(int x)
{
    return x + 1;
}


In terms of speed, memory use etc.
I want to know general efficiency, I know it will obviously vary with different uses and circumstances.

If neither are particularly easier in a certain situation, which should I use in terms of efficiency, and what do you prefer to use?

Thanks in advance if you respond,
exitcode
Last edited on
It doesn't matter for these two functions, since they are so trivial:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ptr_Func(int *x)
{
    *x = *x+1;
}
int val_Func(int x)
{
    return x + 1;
}

int x = 1;
int main()
{
    ptr_Func(&x);

    x = val_Func(x);
}
main:
        addl    $2, x(%rip)   # x = x + 2
        xorl    %eax, %eax    # return 0
        ret


In general, returning values that didn't previously exist is best by value: look at that operator+ you're calling.

There are cases you may need to pass pointers (or, to be more C++ish, iterators): look at std::sort and other algorithms.
Last edited on
For anything though, however complex which is generally more efficient?

Why is returning new values generally better if it uses more memory?

Neither is longer to write, so I want to know before I start a new project, when the actions I am doing are neutral to both, which is faster/more efficient?

If I am starting a large project which involves this kind of situation, this would be convenient to know. I don't want to spend huge amounts of time clearing up my code, for a game for example.

Thanks for the reply though.
It only matters when function span compilation units. Modern C++ makes heavy use of inline functions. The compiler will re-write and completely inline both of your functions. Even if it can't inline, the compiler will perform RVO (http://en.wikipedia.org/wiki/Return_value_optimization). The only time it matters is if the function body is only available in another compilation unit and the compiler cannot inline it. Even then, for the trivial example you give, the cost of the function call is what matters, not how the values are returned. It matters when you are returning complex objects (std::string, for example) and copy constructors are invoked.

When spanning compilation units, look at how the STL is written, or Boost. Most of that code is in header files so it can be inlined. Yours should too. This whole interface/implementation (source/header) split tends to be overused in C++. It only makes sense when it is more important to hide the implementation details from the compiler for code maintenance that it is to expose the implementation to the compiler for performance.

My code is typically header-only, unless I am writing a Pimpl, an implementation for an abstract base class, or main(), or I have static member variables that need to be initialized. And the Pimpl and virtual implementation are going to suffer from indirect function call overhead.
To add to PanGalactic's good post, even if you somehow defeat RVO, inlining, and link-time optimization, under current C++ rules returning std::string will still not copy anything (the return statement calls the move constructor where provided, and std::string provides one)
Returning values sounds better for larger programs, as it can be used in more circumstances, not just one.

For my smaller functions, I will probably use pointers, but for larger ones I intend to reuse, it sounds like returning values is better, because you can do everything you can with pointers and more, and if someone else will want to use your function later, it will be easier to understand for them, and easier to use.

Edit: This is off-topic, I know, but does re-assigning a variable use more memory then redefining it through pointers/references? I was wandering why you would use pointers in that circumstance, and I get the impression it is to save memory, and redefining the variable without pointers uses more memory.

(I'm a beginner, reading jumping into C++)

Thanks again!
Last edited on
Redefining a variable...? Do you mean re-assigning, maybe?
Sorry, re-assigning, changed post.
Topic archived. No new replies allowed.