Defining Our Own Functions
Built-in functions in Python give us a lot of capabilities. We gain even more coding potential by writing our own functions.
We’ll start by looking at the general syntax.
Function Syntax
To create a function, use the following syntax:
|
|
def
is a keyword that instructs Python to create a new function.
Following def
is the function name, which is the label that Python
attaches to the code. When writing our own functions, the programmer chooses the function name. Like any
other variable, a function name should describe the purpose of the function. The stronger we make the name,
the less confusion we add to our code. A list of good naming practices appears
in the next section.
Following the function name, we define parameters within the parentheses. Parameters are variables that can only be used within the function itself. We can define a function with one parameter, multiple parameters, or no parameters at all.
The number and names of the parameters depends on what we want the function to do. Parameters specify what information, if any, the function needs in order to do its job.
The def
statement ends with a colon.
Naming Functions
Python function names should follow these rules:
Names CANNOT match any Python keyword like
print
orfor
.TipMost code editors use syntax highlighting to indicate keywords. Paying attention to the highlighting helps avoid this naming mistake.
Names use only lowercase letters.
The name should describe exactly what the function does.
TipYou should prefer long, descriptive names over short, abbreviated names. If someone unfamiliar with your code can read the function name and tell you what it does, then you chose a good name.
For names with multiple words, separate the words with underscores (e.g.
convert_score_to_percentage
).
Good
convert_feet_to_meters
is_valid_number_entry
draw_square
Not Good
convert
is_valid
draw
Function Code
After the def
statement comes the function body. This is where we code
the action that the function carries out. The function body can contain any
amount of code (statements, loops, conditionals, etc.), but the lines must be
indented when compared to the def
keyword. Python recognizes the end of the
function body once it finds the first unindented line after the def
keyword.
|
|
Line 1 defines the function name and parameter. Lines 2 - 7 are part of the function body. Line 9 is even with the def
keyword, so it is NOT part of the add_numbers_together function
. Since the function add_numbers_together
is defined but not called, only the message "Hello, World!"
will print. The function itself will not execute until it is explicitly called in the code.
Defining vs. Calling
When we define a function, we make it available for later use. However, a function does NOT run when it is defined. It must be called in order to execute.
Check Your Understanding
What are the parameters of the following function? Select ALL that apply.
|
|
perimeter_of_square
side_length
print
For the same code sample, what are the arguments sent by the function call? Select ALL that apply.
square
side_length
4