Module 3 Lesson 3: Parameters and Return Values
Turn your functions into powerful processing engines. Learn how to pass data into functions and get results back using return.
Module 3 Lesson 3: Parameters and Return Values
Functions are most powerful when they are Dynamic. Instead of always printing "Hello," a function should be able to print "Hello Alex," "Hello Sara," or "Hello [Any Name]." To do this, we use Parameters (Inputs) and Return Values (Outputs).
Lesson Overview
In this lesson, we will cover:
- Parameters: The placeholders for information.
- Arguments: The actual data you pass in.
- The
returnKeyword: How a function gives data back. - Returning Multiple Values: Using tuples for complex results.
1. Parameters (Input)
A parameter is a variable defined inside the function's parentheses. It acts as a placeholder for the data the function needs to work.
def greet_user(username): # 'username' is the parameter
print(f"Hello, {username}! Welcome back.")
greet_user("Alex") # "Alex" is the ARGUMENT (the actual data)
greet_user("Sara") # "Sara" is the argument
2. Multiple Parameters
You can pass as many pieces of data as you want, separated by commas.
def calculate_area(width, height):
area = width * height
print(f"The area is {area}")
calculate_area(10, 5) # Output: 50
Order Matters: In the example above, 10 goes into width and 5 goes into height because of their positions.
3. Return Values (Output)
Sometimes, you don't want a function to print a result. You want the function to calculate something and give the answer back to you so you can use it in your code. We use the return keyword for this.
def add(a, b):
return a + b # The function "ends" here and gives back the result
# Now we can store that result in a variable!
result = add(5, 3)
final_score = result * 10
print(final_score) # Output: 80
Important: Once a function hits a return statement, it stops immediately. Any code after return will never run!
4. Return vs. Print
This is a common beginner confusion.
print: Shows text to the human user. The program cannot "use" the text later.return: Gives data back to the program. The human doesn't see it (unless youprintit), but the computer can work with it.
Practice Exercise: The Math Machine
Create a file named math_machine.py.
- Define a function named
squarethat takes one number and returns its square (number * number). - Define a function named
averagethat takes two numbers and returns their average. - Store the result of
square(10)in a variable. - Print a message like:
"The square of 10 is [Result]." - Call
averagewith 20 and 40, and print the result.
Quick Knowledge Check
- What is the difference between a parameter and an argument?
- What happens to a function execution when it hits a
returnstatement? - Why would you use
returninstead ofprint? - If a function has two parameters
xandy, can you call it with only one argument?
Key Takeaways
- Parameters are inputs; Return values are outputs.
- Use
returnto pass results back to the main program. - Once
returnis executed, the function stops. - A function can take multiple parameters but can only return one "thing" (though that thing can be a list or tuple!).
What’s Next?
What if a function could have a "default" answer if you don't provide one? In Lesson 4, we’ll look at Default and Keyword Arguments to make our functions even more flexible!