What you will learn
  • Explain functions and decomposition in plain language.
  • Trace information from input to output.
  • Test one subsystem independently.
  • Recognize a common failure and respond safely.
  • Modify the example without changing several variables at once.

The central idea: functions and decomposition

A function gives a named interface to a reusable operation. Parameters carry inputs into the function, and return sends a result back to the caller without deciding how it will be displayed.

Treat the system as a chain of observable inputs, explicit processing, a decision, and an output. That makes the design easier to explain and gives you a place to test when something fails. A working result is not enough: record what input produced it and what evidence shows the result is correct.

Follow the information through the system

Read the diagram from left to right. Each arrow is a boundary where data, electrical signals, or commands move between parts. Test those boundaries separately before combining the whole system.

A conversion function calculates Fahrenheit for any Celsius value. A separate line decides whether to print, save, or graph the returned number.

  • Give each function one clear responsibility.
  • Use parameters instead of hidden global values.
  • Return data; let the caller choose presentation.
arguments → function(parameters) → return value

A small working example

Run this example exactly as shown before modifying it. Predict the output first, compare that prediction with the real result, and change only one part at a time.

Python
def celsius_to_fahrenheit(celsius):
    return celsius * 9 / 5 + 32

result = celsius_to_fahrenheit(20)
print(result)

The parameter receives 20, the function returns a number, and the caller prints it.

Expected result: 68.0

Test before you trust the result

Start with a normal case, then test a boundary case and a failure case. For software, try empty, unexpected, and extreme inputs. For hardware, begin with wheels raised or actuators disconnected, keep a physical power switch within reach, and verify safe stopping behavior.

Write down the expected result before each test. If the observation differs, stop and find the first boundary where the expected signal disappeared. This method is slower than changing many things at once, but it produces evidence instead of guesses.

  • Test one input with a known value.
  • Observe the intermediate value before the final action.
  • Confirm a safe response when input is missing or invalid.
  • Repeat the same test to check consistency.

Important terms

Function
A named block of reusable code.
Parameter
A name in a function definition that receives input.
Argument
A value supplied when calling a function.
Return value
The result sent back by a function.
Scope
The region where a name can be accessed.

Mini project: practice functions and decomposition

  1. Write a function signature on paper.
  2. Name its input and result.
  3. Implement one calculation.
  4. Call it with three values.
  5. Check one result by hand.

Common mistakes and debugging

  • Printing inside every function instead of returning data.
  • Writing one function that performs unrelated jobs.
  • Forgetting to call the function.
  • Depending on a variable that exists only outside the function.

Independent challenge

Write average(values) that rejects an empty list with a clear ValueError and returns the arithmetic mean otherwise.

Check your understanding: 10 questions

  1. What is the main purpose of functions and decomposition?

  2. What does “Function” mean in this lesson?

  3. How is “Parameter” used?

  4. Why should you test one boundary or subsystem at a time?

  5. What should the system do when an input is missing or invalid?

  6. In your own words, what does “Function” mean?

  7. In your own words, what does “Parameter” mean?

  8. In your own words, what does “Argument” mean?

  9. In your own words, what does “Return value” mean?

  10. In your own words, what does “Scope” mean?

Quiz answers

Reveal all 10 answers after your attempt
  1. A function gives a named interface to a reusable operation. Parameters carry inputs into the function, and return sends a result back to the caller without deciding how it will be displayed.
  2. A named block of reusable code.
  3. A name in a function definition that receives input.
  4. It reveals where the expected value or behavior first changes, so debugging is based on evidence.
  5. It should reject the input, report the problem, or move to a documented safe state instead of guessing.
  6. A named block of reusable code.
  7. A name in a function definition that receives input.
  8. A value supplied when calling a function.
  9. The result sent back by a function.
  10. The region where a name can be accessed.

Summary

functions and decomposition becomes manageable when you identify the input, processing, decision, output, and evidence of success. Build the smallest version, test it, then make one measured change.

Continue learning

Continue to the next lesson, which builds on this model with one additional capability.

Choose a connected learning path

Sources and further reading

Prepared 2026-10-02. Editorially completed for the course. Software examples are designed for current Python 3; verify board, operating-system, library, and hardware details against the linked primary documentation before a physical build.