Functions
Parameters and return values
Pass values into a function and return a result for the caller to use.
Lesson 25 of 63
Reading: about 2 min
Lesson + practice: about 13 min
Difficulty: Beginner
On this page4 sections
A parameter names an input inside a function. An argument is the value supplied by the caller. return sends a result back; printing only displays it and does not make it available for later calculation.
def total_minutes(sessions, minutes_each):
return sessions * minutes_each
planned = total_minutes(3, 25)
print(planned)
print(total_minutes(2, 30) + 15)75 75
Use a default argument
A default parameter supplies a value when the caller leaves that argument out. Put non-default parameters before default parameters.
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Sam"))
print(greet("Sam", "Welcome"))Hello, Sam! Welcome, Sam!
Why this works
Parameters are local names for values passed into a function. A function can return a result without displaying anything; the caller decides whether to print, store, or use it in another expression. A default argument is used only when that argument is omitted.
Practice
Write a function that accepts hours and minutes and returns the total number of minutes. Call it with 1 and 30 and print the returned 90.
Why return a calculated value instead of only printing it?
Reveal answer
The caller can store and reuse a returned value in later calculations.
Try another case
What does total_minutes(2, 45) return in the lesson example?
Check your answer
Reveal answer
It returns 90, because 2 multiplied by 45 is 90.
Your progress
Reveal the check answer or reach the end to complete this lesson.