I am trying to understand pure functions, and have been reading through the Wikipedia article on that topic. I wrote the minimal sample program as follows:
#include <stdio.h>
static int a = 1;
static __attribute__((pure)) int pure_function(int x, int y)
{
return x + y;
}
static __attribute__((pure)) int impure_function(int x, int y)
{
a++;
return x + y;
}
int main(void)
{
printf("pure_function(0, 0) = %d\n", pure_function(0, 0));
printf("impure_function(0, 0) = %d\n", impure_function(0, 0));
return 0;
}
I compiled this program with gcc -O2 -Wall -Wextra
, expecting that an error, or at least a warning, should have been issued for decorating impure_function()
with __attribute__((pure))
. However, I received no warnings or errors, and the program also ran without issues.
Isn't marking impure_function()
with __attribute__((pure))
incorrect? If so, why does it compile without any errors or warnings, even with the -Wextra
and -Wall
flags?
Thanks in advance!