Function Inputs Slides
Computing Foundations • Python
1 / 21
Functions Day 3:
Giving Information TO a Function
Making Our Functions More Flexible
Parameters: IN return: OUT
Learning Targets
- I can explain why a function might need information from the rest of the program.
- I can identify a parameter in a function definition.
- I can pass an argument into a function when I call it.
- I can explain the difference between a parameter and an argument.
45-Minute Lesson
Review: What We Already Know
2 / 21
def coin_flip():
result = random.choice(["heads", "tails"])
return result
Then in our main program:
tiebreaker = coin_flip()
Discuss:
- What does
return do?
- What value could be stored in
tiebreaker?
- Is information moving INTO the function or OUT of the function?
return sends information OUT of a function.
The New Problem
3 / 21
Right Now
Our coin_flip() function does the exact same thing every time.
Upgrade Goal
What if we want the player to CHOOSE heads or tails?
player_choice = input("Choose heads or tails: ").lower()
How could we give that choice TO the function?
Parameters Let Functions Receive Info
4 / 21
def coin_flip(choice):
What is choice?
choice is a parameter.
A parameter is a variable that receives information when the function is called.
Plain-Language Definition
A parameter is a placeholder for information the function will receive later.
Calling the Function With Information
5 / 21
coin_flip("heads")
"heads" is the argument — the actual information sent into the function.
Parameter
The variable in the function definition.
def coin_flip(choice):
Argument
The actual value sent into the function when it is called.
coin_flip("heads")
Trace It
6 / 21
def coin_flip(choice):
print(f"You chose {choice}.")
coin_flip("heads")
Output:
You chose heads.
1
The function is called with "heads".
2
"heads" is placed into parameter choice.
3
Inside the function, choice contains "heads".
4
The function prints: You chose heads.
Connect It to Variables
7 / 21
What does this remind you of?
Regular Variable
name = "Alex"
Stores a value in a named container.
Parameter in a Function
def greet(name):
print(f"Hello, {name}!")
Receives a value when the function is called.
Parameters behave like variables inside the function. They store information temporarily while the function runs.
Back to the Coin Flip
8 / 21
def coin_flip(choice):
result = random.choice(["heads", "tails"])
print(f"The coin landed on {result}.")
if result == choice:
return True
else:
return False
Step 1: Receive
The function receives the player's choice in choice.
Step 2: Compare & Return
It compares the random flip result with choice and returns True or False.
Call the Function
9 / 21
player_coin_choice = input("Choose heads or tails: ").lower()
won_tiebreaker = coin_flip(player_coin_choice)
What goes INTO the function?
player_coin_choice
What comes OUT of the function?
True or False
Trace the Whole Process
10 / 21
player_coin_choice = "heads"
won_tiebreaker = coin_flip(player_coin_choice)
Suppose the coin lands on "heads".
Information moves IN and OUT!
1."heads" is stored in player_coin_choice
2."heads" is passed INTO the function
3.Parameter choice becomes "heads"
4.The random result is "heads"
5.result == choice is True
6.The function returns True OUT
7.True is stored in won_tiebreaker
Apply It to Rock, Paper, Scissors
11 / 21
if player == computer:
print("It's a tie!")
player_coin_choice = input("Heads or tails? ").lower()
won_tiebreaker = coin_flip(player_coin_choice)
if won_tiebreaker:
print("You win the tiebreaker!")
player_score += 1
else:
print("Computer wins the tiebreaker!")
computer_score += 1
TO the function
The main program gives player_coin_choice to coin_flip.
BACK to the program
The function gives True or False back.
Information Flow
12 / 21
Main Program
Argument
Function Parameter
Function Runs
return
Main Program
Coin Flip Example
"heads" choice coin flip True won_tiebreaker
Quick Check: Parameter vs. Argument
13 / 21
def greet(name):
print(f"Hello, {name}")
greet("Jordan")
1. What is the parameter?
name
2. What is the argument?
"Jordan"
Another Example
14 / 21
def double(number):
return number * 2
answer = double(5)
Trace & Identify:
- What is the parameter?
- What is the argument?
- What gets returned?
- What value is stored in
answer?
Code-Along: Google Colab
15 / 21
1
Change def coin_flip(): to def coin_flip(choice):
2
Compare the random result with choice and return True or False.
3
Call it in your program:
won_tiebreaker = coin_flip(player_coin_choice)
Test Your Code!
Run your updated program several times to make sure both winning and losing tiebreakers work.
Debugging Challenge #1
16 / 21
def coin_flip(choice):
result = random.choice(["heads", "tails"])
coin_flip()
Why will this cause an error?
The function requires a value for the parameter choice, but no argument was provided.
Fix:
coin_flip("heads")
Debugging Challenge #2
17 / 21
def greet(name):
print(f"Hello, {name}")
greet
Why does nothing happen?
The function was never called! It needs parentheses () and an argument.
Correct:
greet("Alex")
Student Practice
18 / 21
Task 1
def cheer(name):
Print: Go, NAME!
Call the function with a name of your choice.
Task 2
def double(number):
Return twice the number.
Store the returned value in a variable and print it.
Task 3
In your own function, identify:
- the parameter
- the argument
Connect Back to the Bigger Picture
19 / 21
Variables
Store data.
Functions
Store reusable code.
Parameters
Send information INTO a function.
return
Sends information OUT of a function.
Building Blocks of Programs
20 / 21
Sequence
Instructions happen in order.
Selection
Programs make decisions.
Iteration
Programs repeat.
Functions
Organize reusable pieces of code.
Functions may contain sequence, selection, and iteration inside them!
Exit Ticket
21 / 21
1. What is a parameter?
2. What is an argument?
3. What is the difference between them?
5. What does return do?
6. How do parameters and return work together to move information through a program?
4. In this code:
def greet(name):
print(name)
greet("Taylor")
- • What is the parameter?
- • What is the argument?