4.8. Functions#
So far we have used a lot of Python functions. You might have noticed that
functions use parentheses (), e.g. the print() function or the
input() function.
We’ll now see that we can write our own functions! Functions are very useful for
Reusing blocks of code
Isolating parts of your program, which can make testing and debugging easier as each function can be tested individually
Composing programs as the logic and structure of the program can be broken down into smaller tasks that are easier to solve
Readability as functions generally result in less code which can make your program easier to read
Functions are defined using the following syntax
def function_name(parameter_1, parameter_2, ... parameter_n):
# code to execute
return result
Take note of the following:
defis a keyword which signifies that this is a function definitionfunction_nameis chosen by you and follows the same naming rules as variableseach
parameter_ihas a name chosen by you and is available as a variable in the functionfunctions don’t necessarily need to be provided a parameter
:is placed after the parameter listthe function’s code block must be indented (just like we do for if statements and loops)
returnsignifies that the next thing will be “given back” to the caller
Example 1: One parameter
Here is a simple function called greet that constructs a greeting. This
function returns a string with Hello <name>.
def greet(name):
return 'Hello {}'.format(name)
print(greet('Alison'))
Hello Alison
This function meets the following specifications. Note that function and variable names are formatted in grey and the variable types are given in brackets.
Name:
greetParameters:
name (string)Return: greeting message (
string)
Example 2: Two parameters
Here is an add function, which takes 2 parameters, x and y. When you
use the function you need to provide it two values. The first value that is
provided to the function will be saved under the variable name x and the
second value will be saved under the variable name y.
def add(x, y):
return x + y
print(add(2, 4))
6
Example 3: Two parameters
Note that the order of the parameters matter. Here our divide function will
divide x by y. So divide(2, 4) gives 2/4 while divide(4, 2)
gives 4/2.
def divide(x, y):
return x/y
print(divide(2, 4))
print(divide(4, 2))
0.5
2.0
This function meets the following specifications.
Name:
divideParameters:
x(intorfloat),y(intorfloat)Return:
xdivided byy(float)
Example 4: No parameters
Functions don’t always need parameters. Here is an example of a function that gives us a list of students. Note that we still need the parentheses!
def get_class_list():
return ['Alice', 'Bob', 'Charlie', 'David']
print(get_class_list())
['Alice', 'Bob', 'Charlie', 'David']
This function meets the following specifications.
Name:
get_class_listParameters:
NoneReturn:
xdivided byy(float)
Question 1
What do you think the output of the following code will be?
def power(x, exponent):
y = x**exponent
return y
z = power(2, 3)
print(z)
62389
Solution
C.
The power function takes two parameters, x and exponent. power(2, 3) means that x = 2 and exponent = 3. x**exponent will then be 2**3, which is 2 to the power of 3. This means the result we return is 8.
Question 2
Which of the following best describes the purpose of the function given below.
import random
def five_tosses():
tosses = []
for i in range(5):
r = random.random()
if r < 0.5:
tosses.append('Heads')
else:
tosses.append('Tails')
return tosses
This program simulates 5 random coin tosses for a fair coin. The results will be stored in a list containing the values
'Heads'or'Tails'. The program will give a different result each time.This program simulates 5 random coin tosses for a fair coin. The results will be stored in a list containing the values
'Heads'or'Tails'. The program will give the same result each time.This program simulates 5 random coin tosses for a fair coin. The results will be stored in a list containing the values
0or1to indicate (0 for heads and 1 for tails). The program will give a different result each time.This program simulates 5 random coin tosses for a fair coin. The results will be stored in a list containing the values
0or1to indicate (0 for heads and 1 for tails). The program will give the same result each time.
Solution
Solution is locked
Question 3
Consider the function below.
def calculate(x, y, z):
return x + y - 2*z
Which of the following will return 8? Select all that apply.
calculate(8, 8, 4)
calculate(2, 6, 0)
calculate(2, 5, 3)
calculate(5, 7, 2)
calculate(0, 10, 1)
calculate(7, 3, 4)
Solution
Solution is locked
Question 4
What’s wrong with the following code snippet?
def multiply(a, b, c)
return a * b * c
print(multiply(2, 4, 7))
This will result in a IndentationError because
line 2should not be indentedThis will result in a SyntaxError because
line 1should have a:at the endThis will result in a NameError because the function
multiplyhasn’t been importedThis will result in a ValueError because the variables
a,bandchave not been definedThis will result in a TypeError because the variables
a,bandcare strings, not integers
Solution
Solution is locked
Question 5
Write a function that converts temperature in Celsius to Fahrenheit.
The conversion formula is as follows:
\(T_{\text{Fahrenheit}} = T_{\text{Celsius}}\times \cfrac{9}{5}+ 32\)
Function specification
name:
celsius_to_fahrenheitparameters:
temperature(intorfloat)return: temperature in Fahrenheit (
float)
Solution
Solution is locked