Download

Passing parameters by value or by constant reference

I just read about passing arguments by reference. At the end of that page it is mentioned, that you should not pass fundamental types (bool, char, int, float) by constant reference and pass by value instead. So which Unreal types (int32, FVector, FTransform, …) should I pass by const reference and which by value (variable), to achieve the best performance?

Similarly I am not really sure, when I should use references and when to use pointers. Although my guess is, that there is no big difference between reference and pointer and I should use what the methods inside the function expect.

Hey AllJonasNeeds,

To answer your second question first, if you are passing a pointer into a function it is always being passed as a reference. So in the case of your question they are one in the same.

For the first question, consider the code “FVector TeleportEndPoint;” This is a variable of an Unreal type, so if your function “void FindTeleportEnd(FVector)” would be considered a pass-by-value that expects the Unreal type of FVector. The important thing to remember is that when you use pass-by-value, it will create a copy of the variable and work on that copy within the function. When the function ends, the copy no longer exists and the original variable remains unchanged. If you use pass-by-reference instead, it passes the original variable into the function and any changes to the variable within the function will change the original variable as well. This will usually be considered more performant since it is not going through the overhead of creating a copy, but can also lead to problems since it gives the function direct access to the variable.

Just replying for future readers.

Q1:
The fundamental types are pretty much what AllJonasNeeds listed. Bool, int, float, char. The one other type I can think of for not bothering to pass by reference is an enum; they’re also a very small data type. Everything else, use a reference. As AllJonasNeeds mentioned, use a const reference when possible so the function can use it, but not change it.

Q2:
I’m not sure why Doug has said that pointers and references are one in the same. In the case of this question, and in pretty much any case, using the right one will work and using the wrong one will always not-work. They serve similar purposes but are definitely two different things. As AllJonasNeeds had suggested when he first asked, when it comes to calling a function you use whichever the function demands.

When it comes to writing a function, use a reference wherever humanly possible. Only use a pointer if you have no other choice, and even then, smart pointers should be used, not raw pointers (though I don’t know how well UE4 plays with smart pointers). References have bug-avoiding restrictions that pointers do not have, and pointer-related bugs are often notoriously irritating.

Further explanation would go a bit beyond the question’s scope, so the tl;dr is simply don’t use pointers unless you know you have to.

That is good for single thread code. But when writing multi threaded code most those rules fall apart very quickly.