15 February 20021

Chapter 3: Boss Statements

Grammar Time So far every Python statement we have written is a grammatically complete imperative sentence with tacit subject "Python." Such statements are called worker statements.

The subject of this chapter is a new type of statement called a boss statement.

Boss statements are grammatically incomplete statements that have the power of altering the thread of execution of your programs.

Python, like you, can only act when it is commanded with a complete sentence. Boss statements need a block of statements to complete them as a command that is coherent to Python. The appelation boss statement applies for two reasons.

A typical boss statment has this form.

boss statement:
    line1
    line2 

    .
    .
    .
    lineN

The indented lines constitute the boss statement's block. A block must have at least one line of code. To do nothing in this block, use the pass keyword.

Can a boss statement appear in another boss statement's block? Yes, this is called nesting and it is ubiquitous in programming.

This chapter will introduce three types of boss statements: conditional logic statements, functions, and the while statement.

We will begin with functions.

Functions

So far, we have met variables, which allow us to store data under a name. Functions allow us to store a procedure under a name. Before we begin, let us review the mathemtical definition of function.

A function consists of three items: a domain, which is its allowable set of inputs, a codomain, which is an allowable set of outputs, and a rule tying each element of the domain to some element of the range.

If \(D\) is the domain of a a function \(f\) and \(R\) is its codomain, we write \(f:D\rightarrow R\). For an element \(x\in D\), we write \(f(x)\) to mean the rule \(f\) applied to \(x\).

One consequence of this definition is that when a function is given the same input, it must produce the same output every time. We say that mathematical functions are consistent.

Let's look at some built-in functions we have met so far. The domain of the len function is the set of all character strings. Its codomain is the nonegative integers. It is mathematically consistent; it calculates the number of characters in th string presented it.

The function bin accepts a nonnegative integer and it returns a string that is a binary representation of that integer.

Question What can you say about ord and chr?

Functions of two variables can be thought of as accepting a tuple containing the variables. For example the function

$$f(x,y) = \sqrt{x^2 + y^2}$$

has domain the set of all two-tuples \((x,y)\) of real numbers and has codomain the real numbers. This principle generalizes to a function with any number of variables.

Python Functions

Mathematical functions can be reprsented as Python functions. However, there is a panoply of Python functions that are not mathematical functions. However, careful crafting of programs can cause many of your functions to be mathematical functions. This is desirable; understanding their actions and debugging with them is much simpler than with their messy brethern.

Let's begin with some examples.

inputs, parameters, and arguments

returning an output

side effects

A Very Un-Mathematical Function, input

The Life Cycle of a Functon Call

Scoping, or What Stays in Las Vegas