python / beginner
Snippet
Defining Functions
Functions are reusable blocks of code. They can take inputs (parameters) and return a result using the return keyword.
snippet.py
1
2
3
4
5
def calculate_area(width, height):return width * heightresult = calculate_area(5, 10)print(f"Area: {result}")
Breakdown
1
def calculate_area(width, height):
Defines a function named calculate_area with two parameters.
2
return width * height
Calculates the product and sends it back to the caller.
3
result = calculate_area(5, 10)
Calls the function with specific values and stores the result.