2. Lesson 4: Functions
Introduction to
Programming
Lesson 1
Designing a
Game
Lesson 8
Sorting and
Searching
Lesson 9
Hardware &
Software
Lesson 2
Working with
Files
Lesson 7
Advanced
Algorithms
Lesson 10
Boolean Logic
Lesson 3
Loops
Lesson 6
Navigating the
Web (?)
Lesson 11
Functions
Lesson 4
Data Types
Lesson 5
Putting It All
Together
Lesson 12
2
3. Recap from last time (I)
• Boolean Logic is a phrase used to describe the way computers think
only in TRUE and FALSE
• AND, NOT, and OR can be used to combine statements together,
but their meanings are a little different from their English meanings
• Remember that OR means one, or the other, or both!
or
Fish only
or
Chips only
Fish and Chips
3
4. Recap from last time (II)
• IF statements allow a computer to perform differently when in
different situations
• Add ELSE to decide what will happen when the IF statement is
FALSE
• Add ELIF when you have more than two cases to choose from
Manchester United wins!
Liverpool wins!
Penalty kicks
4
5. Functions are important to understand
• Computers often use functions to do all kinds of actions, whether it’s
adding up numbers or even just showing us text on the screen
• To understand how we can program computers to do things for us,
we first need to understand more about functions
5
6. A function is like a vending machine
• A function is a reusable section of code that performs a specific task
• Like a vending machine, a function is programmed to accept
something from you (money) and give you something back in return
(a fizzy drink)
6
7. Any task can be made into a function (I)
• When programming, you can write functions to do just about
anything
• For example, you could write a function that will accept two numbers
and add them together for you
Function
7
8. Any task can be made into a function (II)
• When programming, you can write functions to do just about
anything
• For example, you could write a function that will accept two numbers
and add them together for you
Function
• Or you could write a function to accept a calendar date and figure
out its day of the week
Function
8
9. Functions don’t need to accept anything (I)
• Sometimes, functions will perform tasks that don’t require any input
from you
• For example, you could have a function that adds all the numbers
from 1 to 100 and returns the result
Function
5050
9
10. Functions don’t need to accept anything (II)
• Sometimes, functions will perform tasks that don’t require any input
from you
• For example, you could have a function that adds all the numbers
from 1 to 100 and returns the result
Function
5050
• You could even have a function that visits The Telegraph’s website
and returns the top headline
Function
“Drivers face 60mph speed limit on
motorways due to EU pollution rules”
10
11. Writing functions in Python is easy!
• For a function that will say “Happy Birthday!”, you just need to write:
Don’t forget the colon!
def birthday():
print “Happy Birthday!”
11
12. Run this function by typing birthday()
• For a function that will say “Happy Birthday!”, you just need to write:
Don’t forget the colon!
def birthday():
print “Happy Birthday!”
birthday()
• Now that the function is written, every time we run birthday(), , the
computer will wish us Happy Birthday!
Happy
Function
Happy Birthday!
12
13. Use words inside the parentheses to give the
function an input
• We can rewrite our function to say any phrase by adding an input:
def parrot(phrase):
print phrase
Any inputs go here
13
14. Include the input when running the function
• We can rewrite our function to say any phrase by adding an input:
def parrot(phrase):
print phrase
Any inputs go here
parrot(“Happy New Year!”) , the computer will wish us
• Now if we run birthday(),
Happy New Year!
“Happy New Year!”
Function
Happy New Year!
14
15. Exercise: Design a Dice Game (I)
• For this exercise, we’ll write a function of our own to create a simple
dice game in Python
• First, open your internet browser, go to labs.codecademy.com, and
click on the button for Python
15
16. Exercise: Design a Dice Game (II)
• Once the page finishes loading, you should see white space on the
left. This is where we’ll be writing the code for our function
• The black space on the left is where we’ll run our code to start a
game of dice!
We’ll start by
writing code here
We’ll go here
later to play dice
16
17. Exercise: Design a Dice Game (III)
• On the left side, let’s write the code shown below
Don’t forget the colon
Remember to tab these lines over
17
18. Exercise: Design a Dice Game (IV)
• If you look closely, you’ll see some familiar print statements and an
IF statement. Let’s go through this line-by-line
18
20. Exercise: Design a Dice Game (VI)
Pick a random number from 1 to 6
Tell us what we rolled
20
21. Exercise: Design a Dice Game (VII)
Pick a random number from 1 to 6
Tell us what we rolled
Pick another number from 1 to 6
and tell us what you rolled
21
22. Exercise: Design a Dice Game (VIII)
Pick a random number from 1 to 6
Tell us what we rolled
Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won!
22
23. Exercise: Design a Dice Game (IX)
Pick a random number from 1 to 6
Tell us what we rolled
Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won
If your roll was larger, tell us we lost
23
24. Exercise: Design a Dice Game (X)
Pick a random number from 1 to 6
Tell us what we rolled
Pick another number from 1 to 6
and tell us what you rolled
If our roll was larger, tell us we won
If your roll was larger, tell us we lost
If the rolls were the same, tell us we tied
24
25. Exercise: Design a Dice Game (V)
• Now click
and wait for a few seconds
• Once a second yellow arrow appears on the right side, type
to play the game!
25
26. Summary (I)
• A function is a reusable section of code that performs a specific task
• Think of functions like vending machines – they can accept an input
and return an output
• Sometimes functions don’t even need an input to return an output
Input money
Output a fizzy drink
26
27. Summary (II)
• The syntax for writing a function in Python looks like this:
def parrot(phrase):
print phrase
Any inputs go here
• You run this function by typing its name: parrot(“Happy New Year!”)
Happy New Year!
27
28. What to do on your own
1. Go to URL to complete the Codecademy course online
2. Do the practice set on the material learned
1. Take the follow-up quiz to test your understanding
28