A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Functions: Decomposing Problems

College Depth 74 in the knowledge graph ☐ I know this ☆ Set as goal
737topics build on this
324prerequisites beneath it
See this on the map →
Defining and Calling Functions→→Function Design and ContractsFunction Parameters and Argument Passing
functions decomposition design

Core Idea

Functions break large problems into smaller, reusable pieces. Each function should do one thing well (single responsibility). Decomposition reduces complexity, enables testing, and makes code reusable. A well-designed function has a clear name, few parameters, and obvious behavior.

How It's Best Learned

Refactor existing code by extracting repeated logic into functions; practice naming functions to reflect their purpose (calcSum, validateEmail).

Common Misconceptions

That functions must be long or complex; that small functions are inefficient; that every piece of code should be in a function (main logic is okay).

Explainer

You already know how to define and call functions — you can write `def greet(name):` and invoke it with `greet("Alice")`. Decomposition is the *design skill* of deciding when and how to break a larger problem into functions. It is the difference between knowing the syntax of functions and knowing how to use them well.

Consider a program that reads a CSV file of student grades, calculates each student's average, determines letter grades, and prints a formatted report. You *could* write this as 80 lines in a single block. But if the averaging logic has a bug, you have to read through file-parsing and formatting code to find it. If you later need to calculate averages for a different file format, you cannot reuse anything. Decomposition means identifying the distinct sub-tasks — parsing the file, computing averages, converting to letter grades, formatting output — and making each one a function. Each function takes inputs, produces outputs, and does one clearly defined thing.

The guiding principle is single responsibility: a function should have one reason to exist and one reason to change. `calculate_average(scores)` takes a list of numbers and returns their mean. It does not know about files, letter grades, or formatting — those are other functions' jobs. This separation makes each function easy to test in isolation: you can verify `calculate_average([90, 80, 70])` returns `80.0` without setting up any files. It also makes the top-level code read like an outline of the solution: `scores = parse_file(path)`, `averages = [calculate_average(s) for s in scores]`, `grades = [to_letter(a) for a in averages]`, `print_report(grades)`. Someone reading this can understand the program's structure in seconds.

A practical heuristic for when to extract a function: if you find yourself writing a comment like "now calculate the average" before a block of code, that block probably wants to be a function *named* `calculate_average`, eliminating the need for the comment. Similarly, if the same logic appears in two places, extract it — not primarily to save lines, but to ensure that fixing a bug in one place fixes it everywhere. Start with the whole problem, identify 3–5 natural sub-tasks, write a function for each, and compose them in a main flow. This top-down decomposition is one of the most transferable skills in programming, applicable in every language and at every scale.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems

Longest path: 75 steps · 324 total prerequisite topics

Prerequisites (1)

Leads To (2)