Chapter 7. Functions

This chapter introduces functions, a fundamental building block in programming. We show how to define them, how to handle input and output, how to properly use them, and how to treat them as objects.

Basics

In mathematics, a function is written as a map that uniquely assigns an element y from the range R to every element x from the domain D.

This is expressed by f : D → R

Alternatively, when considering particular elements x and y, one writes f : x → y

Here, f is called the name of the function and f(x) is its value when applied to x. Here, x is sometimes called the argument of f. Let's first look at an example before considering functions in Python.

For example, D = ℝ x ℝ  and y = f(x1, x2) = x1 - x2. This function maps two real ...

Get Scientific Computing with Python 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.