Consider a function that constructs and returns a rational-number value. Its inputs arrive as parameter values; its result leaves through the return value. It does not need to change an object elsewhere in the program. section 10.2.2
rat rat_get(signed sign, size_t num, size_t denom) [[
__unsequenced__]] {
rat ret = {
.sign = (sign < 0),
.num = num,
.denom = denom,
};
return ret;
}
The constructor assembles a local `rat` value and returns it. Its `[[__unsequenced__]]` annotation states a property the function definition must satisfy; the annotation does not make an impure definition pure. section 10.2.2 section 16.3
A pure function has no effect other than returning a value, and that value depends only on its arguments. Changing a local value while computing the result is compatible with this rule. The question is whether a call reads or changes state outside its interface. section 10.2.2
A function is not pure if it:
- reads changeable program state by means other than its arguments, including a changeable global object; section 10.2.2
- modifies a global object or retains persistent internal state between calls; section 10.2.2
- performs input or output. section 10.2.2
TAKEAWAY: Express small tasks as pure functions when possible. Their behavior is local enough for a reader to follow and for an optimizer to reason about: once arguments are available, a call can be moved relative to other work, provided its result is ready when needed. Purity alone is not a reason to trade correctness for performance. section 10.2 section 10.2.2 section 16.5
Returning a transformed value
A rational-number operation can take `rat` values, calculate a normalized sum or product, and return another `rat`. Assignments to its local copies do not by themselves alter the caller’s objects. By contrast, a swap that changes the caller’s two `double` objects through pointers is not pure. section 10.2.2 section 11.0
In C23, `[[unsequenced]]`—or the macro-safe spelling `[[__unsequenced__]]`—can communicate the relevant guarantee to callers even when only a declaration is visible. For a function without pointer parameters or a pointer return, the annotated function is pure: its result depends on argument values. With pointers, the attribute has a broader meaning that permits limited interaction through objects visible via those pointers; do not equate every annotated pointer-taking function with the simple pure-function model. section 10.2.3 section 16.3
Verify the guarantee before annotating a definition. In particular, an `[[unsequenced]]` function must not read nonconstant global variables or system state. Floating-point code deserves care because a changeable rounding mode can affect a result without appearing among the function’s arguments. section 16.3


















