Difference between revisions of "Function"

From CS 61A Wiki
Jump to: navigation, search
[unchecked revision][unchecked revision]
 
(expand using http://www.cs.utah.edu/~germain/PPS/Topics/functions.html)
Line 1: Line 1:
A '''function''' is a programming construct that performs computations on values. A function can take in ''argument(s)'' as input and can ''return'' a result.
+
A '''function''' is a piece of code that accomplishes a specific task. It usually takes in ''argument(s)'' as input, does some processing, and ''returns'' a result. Once a function is written, it can be ''called'' over and over again.
 +
 
 +
== Motivation ==
 +
Functions take advantage of [[abstraction]]: we don't care how the function carries out its task, we just care that it gives us a correct result.
 +
 
 +
In addition, functions allow us to reuse code instead of rewriting it to work with different values.
  
 
== Pure function ==
 
== Pure function ==
Line 44: Line 49:
  
 
== Sources ==
 
== Sources ==
 +
* http://www.cs.utah.edu/~germain/PPS/Topics/functions.html
 
* http://inst.eecs.berkeley.edu/~cs61a/sp14/slides/02_6pp.pdf
 
* http://inst.eecs.berkeley.edu/~cs61a/sp14/slides/02_6pp.pdf
 
* http://inst.eecs.berkeley.edu/~cs61a/sp14/disc/discussion01.pdf
 
* http://inst.eecs.berkeley.edu/~cs61a/sp14/disc/discussion01.pdf
 
* http://inst.eecs.berkeley.edu/~cs61a/su12/discussion/discussion10/discussion10.pdf
 
* http://inst.eecs.berkeley.edu/~cs61a/su12/discussion/discussion10/discussion10.pdf

Revision as of 17:24, 31 May 2014

A function is a piece of code that accomplishes a specific task. It usually takes in argument(s) as input, does some processing, and returns a result. Once a function is written, it can be called over and over again.

Motivation

Functions take advantage of abstraction: we don't care how the function carries out its task, we just care that it gives us a correct result.

In addition, functions allow us to reuse code instead of rewriting it to work with different values.

Pure function

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.

Examples

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

Non-pure function

A non-pure function or impure function produces a side effect (e.g., changing external state, printing to screen). Its return value may depend on external state.

Examples

print

The print function displays a value on the screen and returns None.

Functions that modify a variable in another frame

The following get_a() function modifies the variable a in the global frame:

a = 0
def get_a():
    global a
    a += 1
    return a - 1

The following tick function modifies the variable n in the countdown frame:

def countdown(n):
    def tick():
        nonlocal n
        n -= 1
        return n
    return tick

Functions that mutate an object in another frame

The following add_square function mutates lst in the global frame:

lst = []
def append_square(n):
    lst.append(n*n)

Sources