🐍 Python
Beginner
What are Python functions and how do you define them?
Answer
Functions in Python are defined with the def keyword: def greet(name, greeting="Hello"): return f"{greeting}, {name}!". Functions support default parameter values, making parameters optional. Python supports positional and keyword arguments: greet("Alice") or greet(greeting="Hi", name="Alice"). *args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict. Functions are first-class objects in Python — they can be assigned to variables, passed as arguments, and returned from other functions. Functions without a return statement implicitly return None. Use docstrings to document functions: """This function greets a person.""" as the first statement.