# FUNCTION DEFINITION:
def say_hello():
print("----------")
print("Hello")
15 Custom Functions
We saw earlier how we have access to a number of built-in functions provided by Python.
But we have the ability to create our own functions as well.
When we create our own functions, in practice we define the function once, specifying what parameters it should accept and which operations it should perform.
After defining the function, we can call or “invoke” it as many times as we want:
# FUNCTION INVOCATIONS:
say_hello()
say_hello()
say_hello()
----------
Hello
----------
Hello
----------
Hello
Notice, the code inside the function is never reached until or unless we invoke the function.
15.1 Parameters
A function may accept input arguments called parameters which are essentially variable references for whatever values are passed in.
15.1.1 Single Parameter
# FUNCTION DEFINITION:
def display_heading(message):
print("------------------")
print(message.upper())
print("------------------")
# FUNCTION INVOCATIONS:
"hello")
display_heading(
"hello again")
display_heading(
= "hello world"
m display_heading(m)
------------------
HELLO
------------------
------------------
HELLO AGAIN
------------------
------------------
HELLO WORLD
------------------
15.1.2 Multiple Parameters
For a function that accepts multiple parameters, order matters. By default, the function assumes we are passing the parameters in the same order as specified during the function definition.
# FUNCTION DEFINITION:
def display_height(feet, inches):
print("THE HEIGHT IS:", feet, "FEET AND", inches, "INCHES")
# FUNCTION INVOCATIONS:
6, 3)
display_height(
3, 6) # OOPS, WRONG ORDER! display_height(
THE HEIGHT IS: 6 FEET AND 3 INCHES
THE HEIGHT IS: 3 FEET AND 6 INCHES
However it is also possible to pass the parameters in a different order, by explicitly specifying the parameter name during function invocation.
# FUNCTION INVOCATIONS:
=6, inches=3)
display_height(feet
=3, feet=6) # NICE! display_height(inches
THE HEIGHT IS: 6 FEET AND 3 INCHES
THE HEIGHT IS: 6 FEET AND 3 INCHES
15.1.3 Default Parameters
It is possible to specify default parameter values that should be used if the parameter is omitted during invocation:
# FUNCTION DEFINITION:
def motivate(name="everyone"):
print("ROW FASTER,", name.upper())
# FUNCTION INVOCATIONS:
# USES DEFAULT PARAMETER
motivate()
"Cam")
motivate(
"Ben")
motivate(
"Grant")
motivate(
"Michael") motivate(
ROW FASTER, EVERYONE
ROW FASTER, CAM
ROW FASTER, BEN
ROW FASTER, GRANT
ROW FASTER, MICHAEL
When we define a function that has default parameters, if it also has required parameters, within the function definition the required parameters must be listed first, and the default parameters must be listed last:
# FUNCTION DEFINITION:
def format_temp(temp, temp_unit="F"): # OBSERVE ORDER OF PARAMS
= u"\N{DEGREE SIGN}"
DEGREE_SIGN print(f"{round(temp)} {DEGREE_SIGN}{temp_unit}")
# FUNCTION INVOCATIONS:
90.3)
format_temp(
90.3, "C") format_temp(
90 °F
90 °C
15.2 Return Values
So far in all the function examples provided above, we are printing some values, but after the function completes, we lose access to the values.
A function can pass back a value to its caller by using the return
keyword. This is a crucially important technique that allows us store the results in a variable for later.
# FUNCTION DEFINITION:
def enlarge(n):
return n * 100
# FUNCTION INVOCATIONS:
print(enlarge(3))
= enlarge(5)
result print(result)
= 7
my_number = enlarge(my_number)
bigger_number print(bigger_number)
300
500
700
15.3 Function Documentation
When defining our own functions, we’ll want to include some documentation / helpful notes to help other people understand what the function is about, and how to invoke it.
See the notes on Function Documentation for more info.
15.4 Function Testing
When we define our own functions, we will endeavor to test them, to ensure they are working as desired.
See the notes on Unit Testing for more info.