Is it ok to use out parameters in a void function?

is this a valid way of writing functions?

void GetCameraParameters(float& TP_FOV, float& FP_FOV, bool& RightShoulder);

Yes, it is the only way to return several types except if using a struct or class.

Yes you can absolutely do this.
Consider renaming your function. Out parameters are not return values, so you don’t really “get” anything. Just based on your function signature and parameter names, I’m guessing this function is more intended to calculate the values of your out params.

I could be wrong and this is a minor nitpick but there’s a good reason to keep your code as self documenting as possible.

This is not recommended.

  • even if caller don’t modify these variables, you cannot make them const.
  • increases the scope of the variable (must be defined and initialized before calling the function)
  • less obvious than a regular return from a function

Out-parameters are mainly used when it has a significant impact, e.g. on performance (mainly applies to types that use dynamic allocation like FString orTArray.

A good example is the std::getline from STL.

void some_function(std::istream & file)
   std::string buffer;
   while (std::getline(file, buffer)) {

Instead of creating a new string each time (+ allocate memory) one object is used and reallocation takes place only when needed.

If this is a blueprint func then multiple output params are the way to go (they give you named output pins). For pure c++ maybe consider returning a struct, but it’s really up to you. If you want optional out params use pointers rather than references, and only write to them if they’re non-null.

Syntactically valid.

But there are a few considerations.

References aren’t generally used with simple types like int, float or bool. This is because it takes less memory to create a new value than it does to create the reference. Even when passing values as const ref, types like int, bool are passed as-is and more complex types like FVector are passed as const ref. Oftentimes it has no practical impact but it’s still good to know regardless.

I would rather create a struct as a return type for your function or at least as the reference parameter if you want to keep it as void. This would be more in line how Epic programs their functions as well.

If you want a function to return multiple variables and you don’t want to create a struct consider using TTuple, you can return as many variables as you like.


TTuple<int32, float, FName> ATestActor::GetResult()
	return MakeTuple(15, 99.f, NAME_None);

You can read the values like this:

    TTuple<int32, float, FName> Data = GetResult();
	int32 Num = Data.Get<0>();
	float Score = Data.Get<1>();
	FName TheName = Data.Get<2>();

This does not work with BP though, you should have stated that.