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
- Run every small function separately.
- Check its return value.
- Connect two functions through a variable.
- Create the coordinator only after its parts work.
- 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.
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!