Write a function calculate_area() that takes length and width, calculates area (length * width), and returns it. Call it and print the result. (Concepts: multiple parameters, return, default parameters)
def calculate_area(length, width=1):
return length * width
area = calculate_area(5, 3)
print("Area:", area)
print("Default area:", calculate_area(4)) # Uses width=1Students, Day 16 levels up functions with inputs and outputs! def calculate_area(length, width=1):—two parameters, width has a default (1) if not provided. Body: return length * width—multiplies and sends back the result (stops function).
Call calculate_area(5, 3)—passes args to params, stores return in area, print it (15). Call with one arg: Uses default width=1, so 4*1=4. Run: Shows both. Return lets functions compute and give data back—unlike print, which just displays.
In your time, make a function that returns a string (e.g., formatted message). Defaults make calls flexible. This builds math from Day 2 into reusable tools!
Key Concepts:
- Multiple Parameters: Functions can take several inputs, separated by commas (e.g., length, width).
- return: Keyword that exits the function and sends a value back to the caller (e.g., return result).
- Default Parameter: Assigns a value to a parameter (e.g., width=1) used if no argument is provided.
