On this page

Arguments and Return Values

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Understanding How Data Enters and Leaves a Function​

To write modular, maintainable Python programs, you must understand two mechanisms:

These two concepts define a function’s interface: what it needs and what it produces. Clear, predictable interfaces are essential for professional‑quality code.

Positional Arguments​

Positional arguments are matched to parameters based on their order.


def multiply(a, b):

    return a * b



result = multiply(3, 4)

Here, a receives 3 and b receives 4 because of their positions.

Positional arguments are concise and common in simple functions.

Keyword Arguments​

Keyword arguments specify the parameter name explicitly.


def introduce(name, city):

    return f"{name} lives in {city}."



introduce(name="Alice", city="Osaka")

Advantages:

Keyword arguments are widely used in real‑world codebases for clarity.

Default Argument Values​

Default values make parameters optional.


def greet(name="friend"):

    return f"Hello, {name}!"

Usage:


greet()            # uses default

greet("Alice")     # overrides default

Default arguments improve usability and reduce repetitive calls.

Mixing Positional and Keyword Arguments​

You may combine both styles, but positional arguments must come first.


def describe(person, age=30):

    return f"{person} is {age} years old."



describe("Alice")

describe("Bob", age=40)

This pattern is common in functions with one required value and several optional ones.

Variable‑Length Argument Lists​

Some functions must accept an unknown number of arguments. Python provides two mechanisms:

args — Variable Positional Arguments​


def total(*numbers):

    return sum(numbers)



total(1, 2, 3, 4)

numbers becomes a tuple containing all provided arguments.

*kwargs — Variable Keyword Arguments​


def debug(**info):

    return info



debug(status="ok", code=200)

info becomes a dictionary of all keyword arguments.

These tools allow you to design flexible, extensible function interfaces.

Understanding Return Values​

A function may return:

Returning a Single Value​


def square(x):

    return x * x

Returning Multiple Values​

Python returns multiple values as a tuple.


def stats(a, b):

    return a + b, a * b



total, product = stats(3, 4)

This is a powerful feature for functions that naturally produce several related results.

Returning Early​

return ends the function immediately.


def safe_divide(a, b):

    if b == 0:

        return None

    return a / b

Early returns improve clarity by avoiding unnecessary nesting.

Designing Clear Function Interfaces​

Effective functions have:

These principles make functions easier to test, reuse, and reason about.

Summary​

In this lesson, you learned how arguments and return values define the way data flows through a function. You now understand positional and keyword arguments, default values, variable‑length argument lists, and how to return single or multiple values. These concepts form the backbone of modular programming and prepare you for more advanced function patterns in the next lessons.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Defining Functions

Next

Scope & Lifetime