|Non-Confidential||PDF version||ARM DUI0472M|
|Home > Compiler Coding Practices > Functions that return the same result when called with the same arguments|
A function that always returns the same result when called with the same arguments, and does not change any global data, is referred to as a pure function.
By definition, it is sufficient to evaluate any particular call to a pure function only once. Because the result of a call to the function is guaranteed to be the same for any identical call, each subsequent call to the function in code can be replaced with the result of the original call.
Using the keyword
__pure when declaring a function indicates that
the function is a pure function.
By definition, pure functions cannot have side effects. For example, a pure function cannot
read or write global state by using global variables or indirecting through pointers,
because accessing global state can violate the rule that the function must return the same
value each time when called twice with the same parameters. Therefore, you must use
__pure carefully in your programs. Where functions can be declared
__pure, however, the compiler can often perform powerful optimizations,
such as Common Subexpression Eliminations (CSEs).