Difference between revisions of "Pure function"

From CS 61A Wiki
Jump to: navigation, search
[unchecked revision][unchecked revision]
(use definition from su12 disc10)
(expand)
Line 1: Line 1:
A '''pure function''' produces no effects other than returning a value. Given the same arguments, it returns the same value. An example is the <code>add</code> function.
+
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).
 +
 
 +
Contrast a pure function with a [[non-pure function]].
 +
 
 +
== Examples ==
 +
*<code>add</code>
 +
*<code>square</code>
  
 
== Sources ==
 
== Sources ==

Revision as of 16:06, 25 May 2014

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).

Contrast a pure function with a non-pure function.

Examples

  • add
  • square

Sources