User Guide

142
(values that are passed into the function), and whether it should
return a value.
To create a function in Python:
1. Type the keyword def followed by the function’s
name.
2. Type opening and closing parenthesis – ().
3. If you want to accept arguments: type them
between the parentheses and separate each one
with a comma.
4. End the line with a colon.
5. (Optional) Add a string on the next line that
describes the function.
6. Place any lines of code you want to include in the
function on following lines, and use the Tab key to
indent them.
7. If you want to return a value, use the command
return.
For example:
def myFunction():
"Prints a message. This line is ignored."
print("My first function")
def myFunction2(val1, val2):
"Adds two numbers and returns the result."
return val1 + val2
Function names follow the same rules as variable names. They
must start a letter (a–z, or A–Z), and the remaining characters in
the name can be letters, numbers, or an underscore (_).
To call one of your functions from other parts of your code, use
the function name and then parentheses. For example:
myFunction()
z = myFunction2(x, y)