# Blueprint function returning wrong value

**URL:** <https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680>\
**Category:** Blueprint\
**Tags:** return, reproducible-bug, unreal-engine, function, Blueprint, question\
**Created:** [November 15, 2018, 12:39pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680 "2018-11-15T12:39:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HEGI](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hegi/32/1194664_2.png) [@HEGI](https://forums.unrealengine.com/u/HEGI)\
**Post date:** [November 15, 2018, 12:39pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/1 "2018-11-15T12:39:31Z")

</div>

Hello,

I have a pure blueprint function that returns a value based on a simple condition.

![259464-return.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/c/45c5a48d50af60606ed4c0a8c04bec02b13d8439.png)

Now when the condition is not met anymore and the function has no return node, the last valid path is returned.  
In the case of this example, when the condition is false, the node still returns a value of 150.

I wonder if this is expected behavior or a bug ?  
Either returning zeroes or a log warning would make more sense and be less prone to errors.

This can easily be reproduced and i have included a test project in the post that shows the issue.

**[Download Test Project for UE 4.21][2]**

Have a great day!

---

<div class="post-metadata">

**Author:** ![redbox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@redbox](https://forums.unrealengine.com/u/redbox)\
**Post date:** [November 15, 2018, 2:07pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/2 "2018-11-15T14:07:10Z")

</div>

Try to make it like this:

```
float MyFunction()
        {
        float Result = 0;
        
        if (Condition)
        {
        Result = 150.0f;
        }
    
    return Result;
    }
```

---

<div class="post-metadata">

**Author:** ![HEGI](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hegi/32/1194664_2.png) [@HEGI](https://forums.unrealengine.com/u/HEGI)\
**Post date:** [November 15, 2018, 2:22pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/3 "2018-11-15T14:22:47Z")

</div>

Adding another return node will instantly solve the problem.  
The blueprint should exactly work as the code you posted there.  
If the condition is not met, it would basically return the float as 0 without the need to define a extra local variable . Like having a virtual return node.

---

<div class="post-metadata">

**Author:** ![redbox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@redbox](https://forums.unrealengine.com/u/redbox)\
**Post date:** [November 15, 2018, 2:25pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/4 "2018-11-15T14:25:10Z")

</div>

Convert it into C++ with blueprint nativization and check how it looks there.

---

<div class="post-metadata">

**Author:** ![HEGI](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hegi/32/1194664_2.png) [@HEGI](https://forums.unrealengine.com/u/HEGI)\
**Post date:** [November 15, 2018, 2:37pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/5 "2018-11-15T14:37:25Z")

</div>

Interesting, the nativized code doesn’t mess with the reference parameter at all when the condition is not met.

```
void AReturnTestBP_C __pf1010915279::bpf__ ReturnTest__pf(/*out*/ float& bpp__ReturnxValue__pfT)
{
	int32 __CurrentState = 1;
	do
	{
		switch( __CurrentState )
		{
		case 1:
			{
				if (!bpv __Condition__ pf)
				{
					__CurrentState = -1;
					break;
				}
			}
		case 2:
			{
				bpp __ReturnxValue__ pfT = 150.000000;
				__CurrentState = -1;
				break;
			}
		default:
			break;
		}
	} while( __CurrentState != -1 );
}

```

So basically it would have to look something like this for it to work properly.

```
                 if (!bpv __Condition__ pf)
                 {
                     bpp __ReturnxValue__ pfT = 0.000000;
                     __CurrentState = -1;
                     break;
                 }
```

---

<div class="post-metadata">

**Author:** ![redbox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@redbox](https://forums.unrealengine.com/u/redbox)\
**Post date:** [November 15, 2018, 2:51pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/6 "2018-11-15T14:51:44Z")

</div>

Well, if you don’t want to use local variable, you can write like:

```
float MyFunction()
{

if (Condition)
{
return 150.0f;
}

return 0.0f;
}
```

---

<div class="post-metadata">

**Author:** ![HEGI](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hegi/32/1194664_2.png) [@HEGI](https://forums.unrealengine.com/u/HEGI)\
**Post date:** [November 15, 2018, 3:01pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/7 "2018-11-15T15:01:26Z")

</div>

Thanks. Just thinking for Blueprints it would be more convenient to be able to just skip the return path and have some sort of virtual return node.  
In C++ making such a mistake, like forgetting a return path is pretty much impossible.

---

<div class="post-metadata">

**Author:** ![ThompsonN13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThompsonN13](https://forums.unrealengine.com/u/ThompsonN13)\
**Post date:** [November 15, 2018, 3:02pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/8 "2018-11-15T15:02:02Z")

</div>

from a non programming perspective i would say that the value is statically set. i realize that the value shouldnt be set from a code perspective until the node is called but its probably one of those quirks of blueprint.

---

<div class="post-metadata">

**Author:** ![HEGI](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hegi/32/1194664_2.png) [@HEGI](https://forums.unrealengine.com/u/HEGI)\
**Post date:** [November 15, 2018, 3:06pm UTC](https://forums.unrealengine.com/t/blueprint-function-returning-wrong-value/437680/9 "2018-11-15T15:06:26Z")

</div>

I think this is very prone to errors in Blueprint. Now after looking at the nativized code, i realized it uses a void function with a pass-by-ref parameter and not a float function(Which does make sense). In C++ the code below does the same as Blueprints, but there you would be alot more careful. It’s not that big of an issue, just alot more convenient and easier to debug if you forget a return node in a complex blueprint function.

```
void ReturnTestFunction(bool bCondition, float& ReturnValue)
{
	if (bCondition)
	{
		ReturnValue = 150.0;
	}

	UE_LOG(LogTemp, Log, TEXT("Return Value: %f"), ReturnValue);
}
```
