Module 16 [Adv]: Parameters & Return

Make functions flexible with inputs and outputs!

Parameter Power

Functions can accept inputs called parameters. Click the highlighted parts related to the parameter `person_name` to see how it works. This enhances Abstraction.

def greet_person(person_name): print("Hello, " + person_name + "!")

Identify the 2 parameter-related parts!

Argument Architect

When you *call* a function that expects a parameter, you provide an argument. Drag the pieces to call the `greet_person` function with the argument `"Bob"`.

Reminder: The function is defined as:

def greet_person(person_name): print("Hello, " + person_name + "!")

Construct the function call with the argument here:

Drop pieces here...

Return Runner

Functions can send back results using the `return` keyword. 1. Define `multiply(x, y)` that returns the product of `x` and `y`. 2. Call it with `4` and `5`, store the result in a variable `product`. 3. Print the `product` variable. This applies Pattern Recognition (multiplication is a pattern).

Output will appear here...

Code Challenge: Simple Calculator

Combine your skills! Define two functions, `add(a, b)` and `subtract(a, b)`, both returning their results. Then, calculate `10 + 5`, store it, and subtract `3` from that result, printing the final answer. This uses Decomposition (add, subtract) and Abstraction.

Calculator Steps

  1. Define `add(a, b)` that returns `a + b`.
  2. Define `subtract(a, b)` that returns `a - b`.
  3. Call `add` with `10` and `5`, store the result in a variable (e.g., `sum_result`).
  4. Call `subtract` using `sum_result` and `3`, store the result in another variable (e.g., `final_result`).
  5. Print the `final_result`.

Module 16 Advanced Practice Complete!

Fantastic! You've mastered passing data into functions and getting results back. This is key for building complex programs.
Check the Video Lesson or head back to the Course Menu.