Professional Documents
Culture Documents
Lesson 6
The full series and 100s of other resources are available from
www.teachingcomputing.com
Series Overview
Information/Theory/Discuss
Task (Code provided)
Challenge (DIY!)
Suggested Project/HW
*Please note that each lesson is not bound to a specific time (so it can be taken at your own pace)
Robotics
reliable
maintainable
Easy to test
def
functionname( parameters ):
"function_docstring"
function_suite
return [expression]
Python has a number of built in functions (like Print(), but we can also
code our own functions and these are called user defined functions.
By default, parameters have a positional behaviour and you need to
inform them in the same order that they were defined.
Video demonstration
And now, lets really get into coding Functions: (Example: create a function and passing parameters)
In this example
we are calling
the
functionprint
function twice
(and passing it
different string
parameters
each time)
Parameters / Arguments
Passing parameters by reference or passing parameters by value
All parameters
(arguments) in
the Python
language are
passed by
reference. It
means if you
change what a
parameter refers
to within a
function, the
change also
reflects back in
Here, we are
maintaining
reference of the
passed object and
appending values in
the same object. So,
this would produce
the following result
[1, 2, 3, 4]]
Here we are
calling the
function with
two parameters.
These are then
printed.
Arguments .
In essence, the words parameter and argument are used interchangeably.
You can call a function by using the following types of formal arguments:
required
# Function sum
keyword
default
variable
We wont get in
to these in
detail in this
lesson, but feel
free to look
them up!
Today I am
Going to expel
Joe Bloggs
Today I am
Going to expel
Joe Bloggs
Analyse the
following code
carefully and see if
you can predict
the output when
both functions are
called.
What will TEST1()
output?
What about
TEST2()?
*Answers on the next slide
Answers:
Num3 is not a
GLOBAL variable
so cannot be
accessed by the
function test2. It
results in an error!
total = 0; #This is a
variable.
# sum is the name of a function
?
def sum( arg1, arg2 ):
# Add both the parameters
and return them."
?
? variable.
total = arg1 + arg2; # Here, total is a local
print ("Inside the function local total : ", total)
return total; Note: It is also possible to store the output of a
# Here we
sum( 10, 20 );
print ("Outside the function global total : ", total)
#the output will be Global total = 0? and Local total = 30
?
Challenge 1:
Discuss and answer the following questions. Be prepared to share!
1. Analyse the code on the left. What does this
program do? Be specific and mention example
inputs and outputs.
Your answer here
def print_options():
print("Make a selection:")
print(" 'p' Print this menu again")
print(" 'c' Convert from Celsius")
print(" 'f' convert from Fahrenheit")
print(" 'q' Exit this program")
def celsius_to_fahrenheit(c_temp):
return 9.0 / 5.0 * c_temp + 32
def fahrenheit_to_celsius(f_temp):
return (f_temp - 32.0) * 5.0 / 9.0
choice = "p"
while choice != "q":
if choice == "c":
c_temp = float(input("Celsius temperature: "))
print("Fahrenheit:", celsius_to_fahrenheit(c_temp))
choice = input("option: ")
elif choice == "f":
f_temp = float(input("Fahrenheit temperature: "))
print("Celsius:", fahrenheit_to_celsius(f_temp))
choice = input("option: ")
else:
choice = "p" #Alternatively choice != "q": so that print
#when anything unexpected inputed
print_options()
choice = input("option: ")
# Program make a simple calculator that can add, subtract, multiply and divide using functions
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
Discussion:
The Big Questions:
Do you think the fact that we see re-used
code and modules in DNA points to
Intelligent design? Why or why not?
Read the quote from author Bill Haines
(below) and discuss. What do you think?
Darwin's time was not only before the advent of computers and computer programming but also before the
discovery of the electron itself. We now know that everything is not the result of natural causes, with the
proliferation of computer programming and apps being a good example and that the genome, is the DNA software
of life- with many reusable code objects (genes) accepting many different design parameters (e.g.. short beaks /
long beaks.)
https://youtu.be/37VwgGRRt5g
https://youtu.be/CGRKqnoQGgM