Anatomy of a Function Definition Abstraction
Functions bundle code. Let's dissect how we define one. Click each highlighted part of the function definition below.
# Code block goes here (indented)
Identify all 6 parts!
Parameter & Argument Matching
When you *call* a function, you pass *arguments* that fill the *parameters*. Click a parameter in the definition, then click the corresponding argument in the call.
Function Definition
# Does something
Function Call
Click a parameter (like 'a')...
Practice Calling a Function
We've defined a function `say_hello` for you below. In the editor, write the Python code to *call* this function, passing the string "AI" as the argument.
print("Hello, " + person_name + "!")
Spot Parameter vs. Argument
Parameters are in the definition, arguments are in the call. Click the parameter in the first line and the argument in the second line. Then check!
Mini Missions: Functions!
Time to build and use your own functions! Complete these tasks. Decomposition helps break tasks into functions.
Mission 1: Announce Topic
Define a function named `announce` that takes one parameter called `topic`. Inside the function, print the phrase "Let's discuss " followed by the value of the `topic` parameter.
Mission 2: Add Two Numbers Decomposition
Define a function `add_numbers` that takes two parameters, `x` and `y`. Inside, it should print the *sum* of `x` and `y`. Then, on a new line *after* the definition, *call* the function with the arguments `5` and `3`.
Mission 3: Repeat Message
A function `repeat_message(msg, times)` is pre-defined (you don't need to write it). Write the code to *call* this function to print the message `"Go! "` (note the space) exactly `3` times.
Module 17 Practice Complete!
Excellent! You've practiced defining functions, using parameters, and passing arguments. This is key for reusable code! Abstraction
Ready for more? Try the Advanced Practice for Module 17.