A python program is made of many lines of code, stored in a file with a name like "example.py". It's natural to have a way to divide the lines 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. The program text will be divided into many functions, each marked in the program text by the word def:
Here is an example Python function named "foo". This function doesn't do anything sensible, just shows the syntax of a function.
def foo(): x = 1 y = 2 z = 3 i = 0
Parts of a function definition:
- def - A function begins with the word
- name - def is followed by the function's name, here
The name is chosen by the programer to reflect what the function does
Here "foo" is just a CS nonsense word
- parenthesis - the name is followed by a pair of parenthesis and a colon
Functions and parenthesis pairs
()frequently go together in Python syntax
- body lines - Indented within the def are the "body" lines of code which make up the function.
When a function runs, the computer runs its body lines from top to bottom.
Key points: A function starts with the word "def", has a name, and some lines of code
Function Call Sequence
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 y = x + 1 foo() # call the "foo" 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 foo() function to run its lines, and then returning to finish the caller() lines.
- Line 1:
x = 6runs in the caller
- Line 2:
y = x + 1runs in the caller
- Line 3:
foo()runs, calling that function
-The run goes over to foo(), running the body lines there
-Note the required parenthesis
()to call the function
-The run of foo() goes through its lines
-When foo() is done, the run resumes in the caller code...
- Line 4
x = 7runs in the caller
The run of lines in the caller code is suspended while called function runs. Or equivalently, we could say that the computer runs one function at a time, so when it's running foo(), it's not running 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.
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.
Key point Call a function by its name with parenthesis added, like
Variant: Obect-Oriented Noun.Verb Function Call
Another way to call a function is the object-oriented aka noun.verb style, like this:
bit is Python data of some sort, and
.move() is a function to run on that data. At its 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.
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 this.
'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:
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
'yellow' are noun modifiers we provide to the action.
Parameters in Parenthesis
The existence of parameters perhaps explains why the parenthesis are part of the function-call syntax — the parenthesis are the place for the parameter values.
print() Parameter Example
This is a "in the interpreter" demo/example. 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. Here are some calls to
print() in the interpreter with their output (see also: interpreter to try this yourself).
>>> print('hi') # Call print() passing in parameter '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
23. The print() function take in that parameter value and prints it to the screen.
A function can accept multiple parameter values, and in fact print() accepts any number of 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
See also: print
Copyright 2020 Nick Parlante