Module 3 Lesson 3: Parameters and Return Values
·Programming

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 return Keyword: 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 you print it), but the computer can work with it.

Practice Exercise: The Math Machine

Create a file named math_machine.py.

  1. Define a function named square that takes one number and returns its square (number * number).
  2. Define a function named average that takes two numbers and returns their average.
  3. Store the result of square(10) in a variable.
  4. Print a message like: "The square of 10 is [Result]."
  5. Call average with 20 and 40, and print the result.

Quick Knowledge Check

  1. What is the difference between a parameter and an argument?
  2. What happens to a function execution when it hits a return statement?
  3. Why would you use return instead of print?
  4. If a function has two parameters x and y, can you call it with only one argument?

Key Takeaways

  • Parameters are inputs; Return values are outputs.
  • Use return to pass results back to the main program.
  • Once return is 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!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn