Module 17: Giving Your Functions Inputs

Learn how to make functions flexible with parameters and arguments.

🔧 Making Functions Reusable

Remember functions? They bundle code for reuse. But what if we want a function to do slightly different things each time we call it? Like greeting different people?

That's where parameters come in! They act like placeholders for information that the function needs to do its job. This makes our functions much more powerful and flexible.

🏷️ Defining Parameters: The Placeholders

When you create a function, you can define parameters inside the parentheses `()`.

def greet(name): # 'name' is the parameter print(f"Hello, {name}!") # Use the parameter inside
📜

Recipe (Function Definition)

Defining `def greet(name):` is like writing a recipe that needs an "ingredient name".

➡️
🏷️

Placeholder (`name`)

The parameter `name` is just a placeholder. It doesn't have a value *yet*.

Parameters are variables listed inside the parentheses in the function definition.

🎁 Passing Arguments: The Actual Values

When you *call* the function, you provide the actual values, called arguments, for the parameters.

1. Function Call

You call the function and provide a value inside the parentheses.

greet("Alice")

2. Argument Passed

The value `"Alice"` (the argument) is passed into the function.

"Alice" ➡️ name

3. Parameter Gets Value

Inside the function, the parameter `name` now holds the value `"Alice"` and uses it.

print(f"Hello, Alice!")

Arguments are the values sent to the function when it is called.

🧠 Computational Thinking Link

Parameters are key to these concepts:

Abstraction

Parameters allow us to abstract the *idea* of greeting someone (greet) from the *specific* person being greeted ("Alice" or "Bob"). We define the general action once and use it with different details (arguments). You don't need to know *how* `print` works inside `greet`, just that it takes a `name`.

Decomposition

When breaking down a big problem (Decomposition), functions represent smaller tasks. Parameters and arguments are how these tasks communicate. One function might calculate a value and pass it as an argument to another function that uses it. E.g., result = calculate_score(data), then display_result(result).

💻 Let's See It Again!

Watch how the argument flows into the parameter.

def add_numbers(num1, num2): # Define function with two parameters
    total = num1 + num2
    print(f"The sum is: {total}")
# Call the function with arguments
add_numbers(5, 3)

Here, add_numbers is defined with two parameters: num1Placeholder for the first number and num2Placeholder for the second number.

When we call add_numbers(5, 3)Calling the function:

  • The argument 5 is passed to the parameter num1.
  • The argument 3 is passed to the parameter num2.

The function then executes, calculating 5 + 3 and printing the result.

🧠 Quick Check!

Module 17 Theory Complete!

You've learned how to make functions flexible using parameters and arguments! This is crucial for writing adaptable AI code.
Ready to practice? Head to the Practice Zone or try the Advanced Practice.