Defining a Function
A function is a block of code with a name. You can run it as many times as you need. You create a function with def. To run it, write its name followed by parentheses. This is called calling the function.
def show_total(item, quantity):
print(f"{item}: {quantity}")
show_total("Books", 3)
show_total("Pens", 5)
Output:
Books: 3
Pens: 5
item and quantity are parameters. A parameter is a name that receives a value when the function is called. The values you pass in a call are called arguments. In the first call, item is "Books" and quantity is 3. In the second call, they are "Pens" and 5.
Writing def does not run the code inside the function (its body). The body runs only when you call the function. Python also has to run the def before it reaches the call.
Functions let you write repeated code once and keep it in one place. A function's name should say what it does. Use lowercase words joined by underscores, like show_total.
In the exercise, define the greeting function. The calls to it are already written.