*Applied Discrete Structures *by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution - Noncommercial - ShareAlike 3.0 United States License.

This document describes the basic ways in which a function can be represented with Sage. These topics are introduced in chapter 7 of *Applied Discrete Structures*.

In any computer program a function defined by a piece of code that will accept inputs and produce outputs. This is a bit more general than the mathematical concept of a *function*, but close enough to let use study the concept through programming. The function on the set $\{0,1,2,3,4,5,6\}$ defined by the ordered pairs $\{(0,0), (1,1), (2,4), (3,2), (4,2),(5,4), (6,1)\}$ can be represented as a dictionary:

2

Not every function can be easily defined by a formula, but this one can be using the standard synatax for Python/SageMath functions.

2

Not every SageMath function is a true function. For example, if the output is governed by a random number generator, the output for a given input will vary. Here is a function that returns 1 with probability $p$ and 0 with probability $1-p$.

[0, 0, 1, 1, 1, 0, 0, 1, 0, 0]