A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. The only result of calling a pure function is the return value. Examples of pure functions are strlen(), pow(), sqrt() etc. Examples of impure functions are printf(), rand(), time(), etc.
If a function is known as pure to compiler then Loop optimization and subexpression elimination can be applied to it. In GCC, we can mark functions as pure using the “pure” attribute.
__attribute__ ((pure)) return-type fun-name(arguments1, âĶ)
{
/* function body */
}
Following is an example pure function that returns square of a passed integer.
__attribute__ _((pure)) int my_square(int val) { return val*val; } |
Consider the below example
for (len = 0; len < strlen(str); ++len) printf("%c", toupper(str[len])); |
If “strlen()” function is not marked as pure function then compiler will invoke the “strlen()” function with each iteration of the loop, and if function is marked as pure function then compiler knows that value of “strlen()” function will be same for each call, thatâs why compiler optimizes the for loop and generates code like following.
int len = strlen(str); for (i = 0; i < len; ++i) printf("%c", toupper((str[i])); |
Let us write our own pure function to calculate string length.
__attribute__ ((pure)) size_t my_strlen(const char *str) { const char *ptr = str; while (*ptr) ++ptr; return (ptr â str); } |
Marking function as pure says that the hypothetical function “my_strlen()” is safe to call fewer times than the program says.
This article is compiled by “Narendra Kangralkar” and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Pure Virtual Functions and Abstract Classes in C++
- Pure virtual destructor in C++
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Difference between Virtual function and Pure virtual function in C++
- SQL | Functions (Aggregate and Scalar Functions)
- Recursive Functions
- Static functions in C
- Write one line functions for strcat() and strcmp()
- Can static functions be virtual in C++?
- Virtual functions in derived classes
- Functions that cannot be overloaded in C++
- Functions that are executed before and after main() in C
- Can virtual functions be inlined?
- Macros vs Functions
- fill() and fill_n() functions in C++ STL
- Return from void functions in C++
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Forward List in C++ | Set 2 (Manipulating Functions)
- List in C++ | Set 2 (Some Useful Functions)
- strtok() and strtok_r() functions in C with examples

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
