9.13 __pure

The __pure keyword asserts that a function declaration is pure.

Usage

A function is pure only if:
  • The result depends exclusively on the values of its arguments.
  • The function has no side effects.
__pure is a function qualifier. It affects the type of a function.

Note

This keyword has the function attribute equivalent __attribute__((const)).
Pure functions are candidates for common subexpression elimination.

Default

By default, functions are assumed to be impure.

Restrictions

A function that is declared as pure can have no side effects. For example, pure functions:
  • Cannot call impure functions.
  • Cannot use global variables or dereference pointers, because the compiler assumes that the function does not access memory, except stack memory.
  • Must return the same value each time when called twice with the same parameters.

Example

int factr(int n) __pure
{
    int f = 1;
    while (n > 0)
        f *= n--;
    return f;
}
Related concepts
4.17 Functions that return the same result when called with the same arguments
4.19 Recommendation of postfix syntax when qualifying functions with ARM function modifiers
Related reference
4.18 Comparison of pure and impure functions
9.31 __attribute__((const)) function attribute
Non-ConfidentialPDF file icon PDF versionARM DUI0375F
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.