Blueprint function returning wrong value

Hello,

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

259464-return.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!

Try to make it like this:

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

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

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.

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;
                 }

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

float MyFunction()
{

if (Condition)
{
return 150.0f;
}

return 0.0f;
}

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.

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.

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);
}