16.4. What are pure and const?
A pure function is one which do not affect
anything outside of it's own scope. This means it may read global
variables or variables to which it was passed a pointer but it may not
write to such variables. It should not read from
volatile variables or external resources (such as
const is a stricter version of
pure, it tells GCC that a function will not read any
data other that of variables that are passed to it. Data cannot be
read by dereferencing a pointer passed to a const
The only effect a pure or
const function can have on your program is it's
return value. Having such a function return void
would make it pointless.
GCC can use this information to perform common
subexpression elimination (!). This means it may call the
function fewer times than it was told to as it knows the outcome will
be the same each time. For example: if you had a function which
converted Celsius to Fahrenheit, and it was placed in a loop
calculating the same value each time, GCC would could replace this
function call with the value returned. GCC knows this is safe if the
conversion function is const.