Python Functions 1.0

A computer program is made up of instructions for the computer. It's natural to have a way to divide the code up into sensible sub-parts, and these are called functions. Almost all the code you work with in Python is inside a function.

Python Function Syntax - "def"

Suppose we have a Python program in the file example.py, there will likely be many functions inside of it, each with part of the code. Each function is marked in the program text by the word "def":

alt: program is made of functions, each with def

Here is an example Python function, just to see its parts

def draw_colors():
    bit = Bit('5x5.world')
    bit.paint('blue')
    bit.move()
    bit.move()
    bit.paint('red')

Rule 1: A function has a name and some lines of code

Function Call 1.0

Say we have a function named "caller", and its body lines are run from top to bottom in the usual way:

def caller():
   x = 6
   draw_colors()   # call the "draw_colors" function
   x = 7
...

To "call" a function is to invoke and run its lines of code. The list below traces the sequence of the caller() function running, calling the draw_colors() function to run its lines, and then returning to finish the caller() lines.

  1. Line 1: x = 6 runs in the caller
  2. Line 2: draw_colors() runs, calling that function
    -The run goes over to draw_colors(), running the body lines there
    -Note the required parenthesis () at the end of the function-call syntax
    -The run of draw_colors() goes through its lines
    -When draw_colors() ended, the run resumes in the caller code...
  3. Line 3 x = 7 runs in the caller

The run of lines in the caller code is suspended while draw_colors() runs. Or equivalently, we could say that the computer runs one function at a time, so when it's running draw_colors(), it's not running the caller lines and vice-versa.

Here's a diagram showing the function-call sequence. The run starts with the lines in the caller function and goes over to run the lines in the called function. When the called function is finished, the run resumes where it left off in the caller.

function call run goes from caller, to called, back to caller

Bonus fact: the variables, such a "x" above, are separate and independent for each function, so x in caller is a completely separate variable from x in foo.

Rule 2: Call a function by its name with ()

Variant: Obect-Oriented Noun.Verb Function Call

Another way to call a function is the object-oriented aka noun.verb style, like this:

    bit.move()

Here bit is Python data of some sort, and .move() is a function to run on that data. At it's heart, this is still just a function call, but the data to work on is listed first, then a dot, then then the function name. Python mixes regular function calls and OOP function calls. Many languages use this "OOP" style, as it has a nice quality of thinking about what data you want to work on first, and then what operation to perform on that data.

Parameter Passing

Suppose we have a paint_window() function that fills a computer window with a color. We want a way to tell the function what color to use when calling it — blue or red or whatever.

A parameter is extra information supplied by the caller that customizes the run of the called function. We will learn all the details of parameters in time. For today, it's sufficient that a function can take a parameter, and the parameter value is "passed in" as part of the call simply by including the desired value within the parenthesis.

So for example to call the paint_window() function to paint the window blue might look like the following.

paint_window('blue')

The syntax 'blue' is a Python string, which is the way to express a bit of text like this.

Calling the function to paint the window yellow would look like:

paint_window('yellow')

A programmer would say the code "calls the paint_window function" and "passes in 'yellow' as the parameter".

You could say that paint_window is the verb we want the computer to do, and 'blue' or 'yellow' are difference noun modifiers we provide to the action.

Rule 3: Pass a parameter value within the parenthesis of a function call

The existence of parameters perhaps explains why the parenthesis are part of the function-call syntax — the parens are the place for the parameter values.

We will see more details of how parameters work, but the case above is pretty simple. Call the function by name, and between the parenthesis there is a possibility of passing in extra information the function uses.

print() Parameter Example

This is a "in the interpreter" demo/example you can try. There is a function in Python called print(). It does many things, but one simple thing it does is take in a parameter value and print it out to the screen. Though unglamorous, this is a way to see function call and parameters in action.

>>> print('hi')     # Call print() with param 'hi'
hi                  # Output produced by print()
>>> print(23)
23
>>> print(4 + 5)
9

What you see here is calling the print() by its name as usual, and in between the parenthesis passing in parameter value like 'hi' or 23. The print() lines take in that parameter value and print it to the screen.

A function can accept 2 or more parameter values. In fact print() will accept multiple parameters. The multiple parameter values are listed within the parenthesis, separated by commas, like this:

>>> print('hi', 22)
hi 22
>>> print('behold', 123, 'donuts')
behold 123 donuts
>>> print(4, 8, 15, 16, 'woot')
4 8 15 16 woot