Reputation: 189
I mean can we declare a function in C as const like
void fun(int iVar) const;
int g_var = 1;
void fun(int iVar)
{
//do some thing
g_var = iVar;
}
Upvotes: 6
Views: 11772
Reputation: 399753
Not in standard C, since there are no classes or objects (as in "class instances, i.e. collections of data with associated functions"), there's nothing for the function to be const
"against".
There could in theory be such a notation to mean "this function has no globally visible side-effects, it's a pure function of its input arguments" but there isn't.
The GNU GCC compiler supports, as an extension, declaring a function as either const
or pure
:
int square(int) __attribute__((pure));
The __attribute((pure))
part is GCC-specific.
Upvotes: 18