# \[C++\]How to return more than two variables from a function?

**URL:** <https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094>\
**Category:** C++\
**Tags:** unreal-engine, functions, question\
**Created:** [November 6, 2023, 8:02pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094 "2023-11-06T20:02:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 6, 2023, 8:02pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/1 "2023-11-06T20:02:18Z")

</div>

I have a function that should take two arguments and return the other two (different types). How can I return them so that I can use them in other C++ functions?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 6, 2023, 8:19pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/2 "2023-11-06T20:19:45Z")

</div>

Make a return type of TTuple\<var1,var2,…\>  
Where vars can any exposed unreal variable types

for example

```auto
TTuple<float,bool> AMyCharacter::DoSomething()
{
  float myfloat_value = 1.0f;
  bool mybool_value = false;
  return MakeTuple(myfloat_value,mybool_value);
}

```

> **[TTuple](https://docs.unrealengine.com/5.3/en-US/API/Runtime/Core/Templates/TTuple/)**

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 6, 2023, 8:24pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/3 "2023-11-06T20:24:06Z")

</div>

Thank you so much!!!

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [November 6, 2023, 9:51pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/4 "2023-11-06T21:51:26Z")

</div>

A more “traditional” way of doing that is using a regular return value, and an Out param, although frankly, for exactly two values, I feel like using a Tuple is _far_ more readable with the intent.

```auto
bool AMyCharacter::DoSomething(float& OutFloat)
{
    OutFloat = 1.0f;
    return false;
}

```

You’ll see that pattern _all over_ Unreal, bringing it up so you recognize it. IMO the Tuple is _much_ cleaner, though. Some might argue that creating a new data structure is unnecessary, so don’t, though. But unless it’s a critical performance area, I’d go with the Tuple.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 7, 2023, 3:38pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/5 "2023-11-07T15:38:50Z")

</div>

[Error]Unrecognized type ‘TTuple’ - type must be a UCLASS, USTRUCT or UENUM  
Why I Cant UFUNCTION?

```c++
UFUNCTION(BlueprintCallable)
TTuple<bool, int32> AddToInvenoty(FName ItemID, int Quantity);

```

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 7, 2023, 4:57pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/6 "2023-11-07T16:57:55Z")

</div>

TTuple is not exposed to blueprints

.h

```auto
USTRUCT(Blueprintable)
struct FInventorytStruct
{
	GENERATED_BODY();

	UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
	bool Added;

	UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
	int32 index;
};

	UFUNCTION(BlueprintCallable)
	void AddToInvenoty(FName ItemID, int Quantity, FInventorytStruct& outValue);

```

.cpp

```auto
void AMyCharacter::AddToInvenoty(FName ItemID, int Quantity, FInventorytStruct& outValue) {

	outValue.Added = true;
	outValue.index = 22;

}

```

![add to inv](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/0/c/10ce23eb4ee82d229c3c3f492d9c522a9689606b.png)  
(after right clicking spit struct for output)

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 8, 2023, 3:53pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/7 "2023-11-08T15:53:32Z")

</div>

And how i can use this is struct on my cpp funcs

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 8, 2023, 3:54pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/8 "2023-11-08T15:54:50Z")

</div>

Either put it in a separate header and include it or put it at the top of your header file if it’s just used with 1 class (has to be above the uclass definition)

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 8, 2023, 3:57pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/9 "2023-11-08T15:57:02Z")

</div>

I mean how to use the value that the function will return if I pass it to &FStructName?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 8, 2023, 3:59pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/10 "2023-11-08T15:59:59Z")

</div>

inside the function when you set the properties of the struct, they will be returned by reference.

Use these return values.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [November 8, 2023, 5:44pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/11 "2023-11-08T17:44:47Z")

</div>

Thank you very much, I just realized that I’m very stupid

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 8, 2023, 5:45pm UTC](https://forums.unrealengine.com/t/c-how-to-return-more-than-two-variables-from-a-function/1369094/12 "2023-12-08T17:45:24Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
