Functions
Define reusable behavior with parameters, return values, and docstrings.
Define a function
Functions give a useful piece of behavior a name. Parameters receive values from the caller, and return sends a result back:
def area(width, height):
"""Return the area of a rectangle."""
return width * height
print(area(4, 3))
Calling a function runs its body. A function that reaches the end without return produces None; printing inside a function is not the same as returning a value.
Default values can make an argument optional:
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
Keep a function focused on one task, choose descriptive names, and document non-obvious expectations. These habits make functions easier to reuse and test.
Exercise: Write is_even(number) that returns True for even integers and False otherwise. Call it with at least two values.
Sources: Python Tutorial: Defining Functions, LearnPython.org: Functions, Google’s Python Class: Introduction