Functions are an incredibly useful tool in programming.
They let us label a block of code so that we can run it by referring to its label
(called the function name).
The function body
is a term meaning the code that is inside the function.
Calling
a function is another way of saying running the function.
In python we call a function by writing the function name and then some
parentheses ()
(brackets), e.g. function_name()
.
Lets look at an example:
# The name of this function is `say_hello`
def say_hello():
print("Hello")
# This is the french way of saying hello
print("Bonjour")
# This code isn't indented so it isn't part of the function body
print("Hi")
# Here we call the function
say_hello()
print("Some other code")
# We can call the function as many times as we want
say_hello()
say_hello()
say_hello()
Copy and run this code and you will see that hello
and bonjour
is
printed 4
times since we call the function four times.
Function arguments
Functions can also take in variables (called arguments
or parameters
).
When we call a function with arguments we must pass in values. We do this
by putting them in the parentheses e.g. say_hello("Sam")
.
# Define a function called `say_hello` which takes in a variable called name
def say_hello(name):
print("Hello " + name)
say_hello("Sam")
say_hello("dog")
This prints out Hello Sam
and then Hello dog
.
Tip: whenever you want to debug a function it is useful to imagine replacing
the arguments of the function with real values such as Sam
or Dog
and then
run through the function in your head to see what it does.
Function return
Functions can return a value, which is useful for calculating things which
we can return to the caller of the function.
def add(a, b):
return a + b
# Here we take the return value of 2+3 and assign it to the variable result
result = add(2, 3)
print(result)
# We can add strings together too
print(add("one", "two"))
# If you uncomment the next line and run it you will get an error (remember we need to convert numbers to strings to join them)
# print(add(42, " is the answer."))
Challenge 1
We're going to make a calculator using our knowledge of functions and conditionals (if
statements).
- Create a function that accepts three values
operator
, a
, b
.
- Check if the operator is
+
(plus), -
(minus), *
(times) or /
(divide).
- For each of the four cases that the operator could be, return the result of
a OPERATOR b
like a calculator.
Let's say the function you create is called calculate
here are some examples of what should happen:
# Should print 5 (since 2+3)
print(calculate('+', 2, 3))
# Should print 11 (since 13-2)
print(calculate('-', 13, 2))
# Should print 2 (since 5/2)
print(calculate('/', 10, 5))
# Should print 40 (since 5/2)
print(calculate('*', 2, 20))
Solution to challenge 1
If you finish then you can take a look at the solution to compare how you did it
to how I did it. Here is it.
Challenge 2
Once you get challenge 1 working adapt it so that a program asks you to enter
an operator, one number and then another number and it prints the result using your calculation function.
This program should keep asking and returning the result forever by using
a while True:
loop, but at the end of each calculation it should ask
the user if they want to stop or not. If the user wants to stop use the break
command to exit the loop.
Extensions:
- If the user enters an invalid operator tell them they must enter a valid operator and go back to the start of the loop
(Hint: use a
continue
statement which skips to the next iteration of the while loop.
You'll also probably want to use and
between the conditions for the if
statement).
- Print out the full calculation and the result in the format
a OPERATOR b = RESULT
.
So if I enter +
as the operator, 2
for a and 5
for b, the program should print 2 + 5 = 7
.
Watch out: don't forget to use the int
function to convert the user input
into a number.
Solution to challenge 2
If you finish then you can take a look at the solution to compare how you did it
to how I did it. Here is it.