๐Ÿ“ Python

How Functions Work Together in Python ๐Ÿงฉ

P
Author
PyLand Team
๐Ÿ“…
Published
09.08.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
77
๐ŸŒฑ
Level
Beginner

A small function normally handles one clear task. A real program, however, contains several tasks: receive data, process it, display a result, and preserve an outcome.

Instead of one enormous function, create several small functions and connect them with ordinary calls.

๐Ÿงฑ One Function, One Task

Start with two independent functions:

def calculate_total(price, delivery):
    return price + delivery


def show_total(total):
    print(f"Total: {total}")

The first function calculates and returns a number. The second receives a finished number and only displays it.

This structure is easy to test:

total = calculate_total(500, 100)
show_total(total)

The data route is:

500 and 100 โ†’ calculate_total() โ†’ 600 โ†’ show_total()

๐Ÿ” One Result Becomes Another Function’s Argument

A value returned with return can be stored and passed to the next function:

def double(number):
    return number * 2


def make_label(number):
    return f"Result: {number}"


doubled = double(7)
label = make_label(doubled)
print(label)

Each line shows a separate stage. For a beginner, this is clearer than nesting several calls inside one expression.

๐Ÿ“ฆ A Function Can Pass a Dictionary

When a result contains several related values, a function can return a dictionary:

def inspect_device(name, errors):
    score = errors * 10
    return {
        "name": name,
        "score": score,
    }

The next function reads the required fields:

def show_result(result):
    print(f"{result['name']}: {result['score']}")


inspection = inspect_device("SENSOR", 2)
show_result(inspection)

Keep source data and results separate: an analysis function should not silently change the dictionary it receives.

๐ŸŽ›๏ธ A Coordinator Function

Once the action order is clear, move it into a coordinator function. It does not repeat calculations; it calls finished functions in the required order:

def run_check(name, errors):
    result = inspect_device(name, errors)
    show_result(result)

One call now starts the full route:

run_check("SENSOR", 2)

The coordinator answers โ€œwhat runs and in which order?โ€ Other functions answer โ€œhow is one operation performed?โ€

A good coordinator:

  • calls existing functions;
  • stores their results in clear variables;
  • passes results to the next operation;
  • does not copy the functions’ internal algorithms.

๐Ÿ”„ A Coordinator and a Loop

To process several matching records, a coordinator can use a loop:

def run_all_checks(devices):
    for device in devices:
        result = inspect_device(device["name"], device["errors"])
        show_result(result)

inspect_device() still checks only one device. The loop belongs in the coordinator because it manages the complete scenario.

๐Ÿงญ A Menu Calls Existing Functions

A menu is another coordinator. It receives a user command and selects an existing action:

def show_report():
    print("Report opened")


def show_help():
    print("Help opened")


def handle_choice(choice):
    if choice == "1":
        show_report()
    elif choice == "2":
        show_help()
    else:
        print("Unknown command")

The branches contain no report-building or help-building code, only calls. If report behavior changes, update show_report() without rewriting the menu.

โš ๏ธ Common Mistakes

Calling a Function Before Its Definition

Python executes a file from top to bottom. Define a function above its first call.

Losing a Returned Result

Store a value when another step needs it:

result = inspect_device("SENSOR", 2)

Repeating Algorithms in a Coordinator

Do not copy the calculation into run_check(). Call the calculation or inspection function that already owns it.

One Function Does Everything

A function that reads input, calculates, prints, mutates a list, and controls a menu is difficult to test. Separate independent tasks.

โœ… Step-by-Step Check

  1. Run every small function separately.
  2. Check its return value.
  3. Connect two functions through a variable.
  4. Create the coordinator only after its parts work.
  5. Confirm that the complete result remains correct.

๐ŸŽฏ Summary

  • one function can call another;
  • a returned value becomes the next call’s argument;
  • a dictionary can carry several related results;
  • a coordinator manages order without copying algorithms;
  • a menu selects and calls existing operations.

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

map() โ€” Transform Every Element!

The map() function lazily applies a transformation to every item in an iterable.

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 428
๐Ÿ“

filter() โ€” Pick What You Need!

The filter() function keeps the elements for which a predicate returns a truthy value.

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 428
๐Ÿ“

Lambda Functions

A lambda is a small, anonymous, single-line function.

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 415
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum