How to create a pure functon in C++?

Hi, I want to Create a pure function in C++ how to do it?

What I am trying is :
void [[gnu::pure]] GetWeight();
please correct me if I am wrong.

	UFUNCTION(BlueprintCallable, BlueprintPure)
	float  myFloatSum(float A, float B);
6 Likes

This will make the function expose to blueprints as a pure function but it will still remain impure on C++ side.

Please correct me if I am wrong.

What is a pure function in C++ for you?

Because definition of a Pure function in code is a function where you give same parameters and get same results ( sin(), sqrt(). flor() ) and impure is one that return different values (random(), time() )

1 Like
#Pure
UFUNCTION(BlueprintCallable, BlueprintPure)
int foo(int n)
{
  return n*2;
}
#Impure
int i = 42;

UFUNCTION(BlueprintCallable)
int bar(int n)
{
  ++i;
  return n*i;
}
1 Like

I thought its something need to pure on C++ side too, and the UPROPERTY is only making it pure on bp side, but now its all cleared I have no doubts anymore on this theory, Thank You for help to learn this.

2 Likes

Thank You for this example and It makes the eldany.uy example more clear to understand. Thank You so much.

2 Likes

I think the definition of Pure in Blueprints is not right because you can make a pure BP function with time, random, modify external variables, whatever inside (impure function) and Unreal accepts it as pure anyway.

Pure definition for BPs is more like a function with no execution pin, but you can actually change everything inside the function anyway.

In C++ pure or impure is a concept on how do you make the function only exactly like the example of @c0r37py

anyway his example is like this:

	UPROPERTY(BlueprintReadWrite)
	int i = 42;
	
	UFUNCTION(BlueprintCallable, BlueprintPure)
	int  bar(int  n)
	{
		i++;
		return n*i;
	}

so in theory IMPURE function but it works perfectly and can use as ‘pure’ in blueprints without
any error.

3 Likes