Consider this little program.

**import** `math`

**def** `sq`(`x`):

**return** `x` ** `2`

**def** `f`(`x`):

`sinx2` = `sq`(`math`.`sin`(`x`))

`x2` = `sq`(`x`)

**return** `sinx2` / `x2`

`print`(`f`(`0.1`))

Here is the order in which this program executes.

- We enter

with`f`

being 0.1.`x` - The

function first computes sin 0.1 ≈ 0.0998, then passes that to`f`

.`sq`- We enter

with`sq`

being 0.0998.`x` - The

function returns the square of that, 0.009967.`sq`

function saves this result, 0.009967, into`f`

.`sinx2` - We enter
- Then

function passes`f`

to`x`

. The question is… what is`sq`

now? In entering`x`

, we just set`sq`

to be 0.0998. However, that was`x`

's`sq`

;`x`

has its own`f`

, which has never changed from 0.1. Since`x`

is passing its`f`

, the number 0.1 is passed to`x`

.`x`- We enter

with`sq`

being 0.1.`x` - The

function returns the square of that, 0.01.`sq`

function saves this result, 0.01, into`f`

.`x2` - We enter
- Finally,

computes the ratio of`f`

and`sinx2`

, 0.9967, and returns that result.`x2`

The important point here is that each function has its own
“world” of variables.
When

stashes something into one of its variables,
it has no effect on variables in `sq`

's world, even if the
variables happen to share the same name.`f`

There is an exception to this: Variables that exist outside
any functions are available to all functions. These are known as
**global variables**, for obvious reasons. For example:

`pow` = `5`

**def** `sq`(`x`):

**return** `x` ** `pow`

Here, the

function refers to `sq`

. This
variable doesn't exist within its own “world,”
so Python looks to the global variables and finds that `pow`

is 5.`pow`

However, if

had any code that `sq`*wrote* to

(i.e., “`pow`

”),
Python would conclude that `pow` = `y`

has its own `sq`

variable,
separate from the global `pow`

.
In practice, this is rarely an issue. (But if you wanted `pow`

to actually change the global `sq`

, you could do that by
inserting a special statement “`pow`

”
into the **global** `pow`

function.)`sq`

In practice, expert programmers disdain global variables,
except for circumstances where the global variable is a
**constant**: That is, where the variable's value never
changes from its initial value, in which case the variable is
just an alternative name for that value.

Sometimes we want functions that don't return anything.
Such functions exist purely for their **side effects**:
Usually, the function displays something.
The built-in

function is a significant example.`print`

For example, suppose we want a function that displays a muliplication table for the numbers 1 through the parameter value.

**def** `print_table`(`n`):

**for** `i` **in** `range`(`1`, `n` + `1`):

`line` = `''`

**for** `j` **in** `range`(`1`, `i` + `1`):

`line` = `line` + `' {0:3d}'`.`format`(`i` * `j`)

`print`(`line`)

`print`(`'multiplication to 3'`)

`print_table`(`3`)

`print`(`'multiplication to 5'`)

`print_table`(`5`)

This function lacks any

statement.
When the program calls
“**return**

”,
the function displays the multiplication table
and then reaches the function body's end. At that point, it
would return, and the calling code would go on to the next line,
“`print_table`(`3`)

”.`print`(`'multiplication to 5'`)

Suppose somebody were to write
“

” for some
reason. Since `x` = `print_table`(`3`)

ends without returning
anything, `print_table`

would get a useless value (namely, `x`

).
In practice, nobody would ever try to use the value from a
function that doesn't return anything.**None**

By the way, one sometimes wants a

statement
that exits the function without having the function return
anything. This is easy enough: Just write “**return**

”
with nothing following it.**return**

**def** `show_sum`(`pow`):

`total` = `0`

**while True**:

`line` = `input`()

**if** `line` == `''`:

`print`(`total`)

**return**

`total` += `float`(`line`) ** `pow`

In this case, when the function is called,
the function reads several lines from the user, adding the
values on the lines. But when the user finally enters an empty line,
the function displays the current value of

, and
then the function ends its computation.`total`