Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of pure function

Tags:

c

pure-virtual

Today i was reading about pure function, got confused with its use:

A function is said to be pure if it returns same set of values for same set of inputs and does not have any observable side effects.

e.g. strlen() is a pure function while rand() is an impure one.

__attribute__ ((pure)) int fun(int i) {     return i*i; }  int main() {     int i=10;     printf("%d",fun(i));//outputs 100     return 0; } 

http://ideone.com/33XJU

The above program behaves in the same way as in the absence of pure declaration.

What are the benefits of declaring a function as pure[if there is no change in output]?

like image 409
Green goblin Avatar asked Jun 22 '12 09:06

Green goblin


People also ask

What are the characteristics of pure function?

A pure function will not cause side effects. It exhibits the two necessary characteristics: The function depends only on its arguments to produce a result. The function does not cause any side effects.

What is meant by pure functions?

In computer programming, a pure function is a function that has the following properties: the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and.

What are pure functions and side effects?

A pure function does not have side-effects and its result does not depend on anything other than its inputs. A pure function guarantees that for a given input it will produce the same output no matter how many times it is called.

What is pure function with example?

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.


2 Answers

pure lets the compiler know that it can make certain optimisations about the function: imagine a bit of code like

for (int i = 0; i < 1000; i++) {     printf("%d", fun(10)); } 

With a pure function, the compiler can know that it needs to evaluate fun(10) once and once only, rather than 1000 times. For a complex function, that's a big win.

like image 110
Philip Kendall Avatar answered Sep 24 '22 17:09

Philip Kendall


When you say a function is 'pure' you are guaranteeing that it has no externally visible side-effects (and as a comment says, if you lie, bad things can happen). Knowing that a function is 'pure' has benefits for the compiler, which can use this knowledge to do certain optimizations.

Here is what the GCC documentation says about the pure attribute:

pure

Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example,

          int square (int) __attribute__ ((pure)); 

Philip's answer already shows how knowing a function is 'pure' can help with loop optimizations.

Here is one for common sub-expression elimination (given foo is pure):

a = foo (99) * x + y; b = foo (99) * x + z; 

Can become:

_tmp = foo (99) * x; a = _tmp + y; b = _tmp + z; 
like image 25
ArjunShankar Avatar answered Sep 25 '22 17:09

ArjunShankar