Functions Basics
Functions are the building blocks of reusable code. They group logic together so we can call it whenever needed, preventing code duplication. A function only runs when it is called.
Defining and Calling
Use the def keyword to define a function. Indentation is critical in Python to define the function body. Variables in the definition are called parameters, while the values we pass in are called arguments.
def greet_user(name):
"""Simple greeting function."""
print(f"Hello, {name}!")
# Calling the function
greet_user("Alice")
Output:
Hello, Alice!