Quick Answer
Proper way to define functions in Python.
Understanding the Issue
Python functions are defined using the def keyword, with parameters in parentheses. Functions can have docstrings, type hints (Python 3.5+), and default arguments. Proper indentation is crucial.
The Problem
This code demonstrates the issue:
Python
Error
-- Need to define a function to calculate area
The Solution
Here's the corrected code:
Python
Fixed
-- Basic function
def rectangle_area(width, height):
"""Calculate area of rectangle."""
return width * height
-- With type hints and defaults
def circle_area(radius: float = 1.0) -> float:
"""Calculate area of circle."""
return 3.14159 * radius ** 2
Key Takeaways
Use docstrings and type hints for better maintainability.