When are functions allowed to be pure and still execute?

I am working on a project, and have a lot of code that does things with arrays, such as resize them, set them, etc… I have been holding back on what I want to do because the code is an absolute mess (I am in a macro with local variables). Just a moment ago, I realized that one of the functions I have can be pure, but didn’t think it would work because the execution wires were being used. After some testing, my function did execute despite being pure, and this brought me to the question of when I can make my functions pure and have them still function properly? When should I not make my functions pure?

A pure function is like a contract you make as a programmer that says, given every variable you put into this function, it will always return the same result such that all the inputs remain the same.

I reserve pure functions for things that i know will adhere to this rule.

I’m not sure I completely understand, could you expound on that a little bit more?

Here is a small example of code i have.

It does not modify any external data in itself and given the same inputs it would provide the same result every time.

Even though there are no execution pins on the Node itself, remember that when Blueprint encounters a Node that it needs to recieve a value from it will track back through that Node chain to produce the value required. If it needs to jump into a Pure function it will execute it just like if it had execution pins.

1 Like

I could be very wrong but here’s my understanding of functions in UE4.

Functions are similar to Custom Events, but they are only compiled once. This means you cannot use nodes that would pause the execution of the function as doing so may force your code to run out of order. A function should be used for data that will be passed without delay, and when you expect to return results from the Function.

Moving on from there …

Something I learned recently: the difference between Pure and Impure functions is how they’re called. An Impure function is only called when Exec pins are connected to it whereas a Pure Function is called anytime data is fed into it.

For example: If I use an Impure Function that returns two results and set each of those results to variables the function would still only be called once. If you did the same thing with a Pure Function the Function would be called every time you received or attached data to it. In the picture I provided the Pure function would be called twice, and the Impure would only be called once. Also, branching result nodes in a Pure Function is the same calling it multiple times.

At least, that’s what I gather. I asked a similar question recently and didn’t get an answer – so hope this helps you out! ( docs Functions | Unreal Engine Documentation )

1 Like

Very helpful