Difference between revisions of "Pure function"

From CS 61A Wiki
Jump to: navigation, search
[unchecked revision][checked revision]
(expand)
(add)
Line 1: Line 1:
A '''pure function''' produces no effects other than returning a value. It cannot depend on external state (i.e., given the same arguments, it always returns the same value).
+
A '''pure function''' produces no effects other than returning a value. Its return value depends only on the inputs. Given the same arguments, it always returns the same value.
  
 
Contrast a pure function with a [[non-pure function]].
 
Contrast a pure function with a [[non-pure function]].
  
 
== Examples ==
 
== Examples ==
*<code>add</code>
+
*<code>add</code> always returns the sum of the arguments and has no side effects.
 
*<code>square</code>
 
*<code>square</code>
  

Revision as of 16:08, 25 May 2014

A pure function produces no effects other than returning a value. Its return value depends only on the inputs. Given the same arguments, it always returns the same value.

Contrast a pure function with a non-pure function.

Examples

  • add always returns the sum of the arguments and has no side effects.
  • square

Sources