Functions in Depth

Python 3.12 · ✓ verified by execution on 2026-07-23

The execution of a function introduces a new symbol table used for the local variables of the function.

python · visualize
x = 10
def change():
    x = 5
change()
print(x)

Missing Returns

In fact, even functions without a return statement do return a value… This value is called None

python
def do_nothing():
    pass
result = do_nothing()
print(result)
Output
None

Default Arguments

The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow.

python
def greet(name, msg="Hello"):
    print(f"{msg}, {name}!")
greet("Alice")
greet("Bob", "Hi")
Output
Hello, Alice!
Hi, Bob!

Keyword Arguments

Functions can also be called using keyword arguments of the form kwarg=value.

python
def describe(color, size):
    print(f"{size} {color}")
describe(size="large", color="red")
Output
large red

Arbitrary Arguments (*args)

Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple

python
def add_all(*numbers):
    print(type(numbers))
    total = 0
    for n in numbers:
        total += n
    return total
print(add_all(1, 2, 3))
Output
<class 'tuple'>
6

Arbitrary Keyword Arguments (**kwargs)

When a final formal parameter of the form **name is present, it receives a dictionary … containing all keyword arguments except for those corresponding to a formal parameter.

python
def print_info(**info):
    print(type(info))
    for k, v in info.items():
        print(f"{k}: {v}")
print_info(name="Alice", age=30)
Output
<class 'dict'>
name: Alice
age: 30

Check yourself

Functions always have to return a value using the return keyword.

Reveal answer

If a return statement is omitted, Python automatically returns None. — If a return statement is omitted, Python automatically returns None.

You can mix positional and keyword arguments in any order when calling a function.

Reveal answer

In a function call, keyword arguments must always follow positional arguments. — In a function call, keyword arguments must always follow positional arguments.

Variables created inside a function are available globally.

Reveal answer

Variables created in a function live in a local symbol table and disappear when the function exits. — Variables created in a function live in a local symbol table and disappear when the function exits.

*args and **kwargs are reserved keywords.

Reveal answer

The single and double asterisks do the work. The names args and kwargs are just strong community conventions. — The single and double asterisks do the work. The names args and kwargs are just strong community conventions.

Challenges

Challenge 1 +15 XP

Write a function make_profile that takes a name and an optional status that defaults to active. It should return a dictionary with those keys.

python
  • Test 1 — expects "{'name': 'Alice', 'status': 'active'}\n{'name': 'Bob', 'status': 'offline'}\n"
Need a hint? (−25% XP)

Use a default parameter value in the function definition: `status="active"`.

Show solution (0 XP)
def make_profile(name, status="active"):
    return {"name": name, "status": status}
print(make_profile("Alice"))
print(make_profile("Bob", "offline"))

Challenge 2 +15 XP

Write a function build_url that takes a base string and arbitrary keyword arguments (**kwargs). It should print the base url followed by a question mark and the kwargs dictionary.

python
  • Test 1 — expects "http://test.com?{'q': 'python', 'page': 2}\n"
Need a hint? (−25% XP)

The **kwargs parameter captures all extra keyword arguments as a dictionary.

Show solution (0 XP)
def build_url(base, **kwargs):
    print(f"{base}?{kwargs}")
build_url("http://test.com", q="python", page=2)