📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Functions

Functions

5 min read Quiz at the end
Define functions with def. Accept parameters and return values with return. Use default values for optional parameters. *args collects extra positional arguments as a tuple, **kwargs as a dictionary.

Defining Functions

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Alice"))           # Hello, Alice!
print(greet("Bob", "Hi"))       # Hi, Bob!

# *args and **kwargs
def log(*args, **kwargs):
    print(args, kwargs)

log(1, 2, key="value")
# (1, 2) {'key': 'value'}
Topic Quiz · 5 questions

Test your understanding before moving on

1. How do you define a function?
💡 Functions are defined with the def keyword in Python.
2. What does return do?
💡 return sends a value back to the caller and exits the function.
3. What are *args?
💡 *args collects extra positional arguments into a tuple.
4. What are **kwargs?
💡 **kwargs collects extra keyword arguments into a dictionary.
5. What is a default parameter?
💡 Default parameters (def f(x=10)) are used when the argument is omitted.