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

Defining and Calling Functions

College Depth 73 in the knowledge graph ☐ I know this ☆ Set as goal
739topics build on this
323prerequisites beneath it
See this on the map →
Variables and AssignmentConditional Statements+1 more→→Algorithm Design BasicsDebugging Basics+8 more
functions def call abstraction modularity

Core Idea

A function is a named, reusable block of code that performs a specific task. Defining a function (using def, function, or similar keywords) specifies what the function does; calling it by name executes that code. Functions promote abstraction by hiding implementation details behind a name, and modularity by separating a program into independent, testable units. Well-named functions make code read like a description of what it does rather than how.

How It's Best Learned

Refactor a long script into smaller functions, one task each. Write each function before calling it. Practice reading function signatures to understand what a function expects and produces.

Common Misconceptions

Explainer

You know how to store values in variables and use conditional statements to make decisions. Functions take these building blocks and give you a way to organize them into reusable, named units. Think of a function like a recipe: the definition is writing the recipe down (ingredients needed, steps to follow, what comes out at the end), and the call is actually cooking the dish. Writing a recipe doesn't produce food — you have to follow it. Similarly, defining a function doesn't execute the code inside it — you have to call the function by name.

In most languages, you define a function with a keyword (`def` in Python, `function` in JavaScript), a name, parentheses for any inputs, and a body of code. For example, `def greet(): print("Hello!")` defines a function named `greet` that prints a greeting. To actually execute it, you write `greet()` — the parentheses are what trigger execution. Without them, you're just referring to the function as an object, not running it. This distinction between defining and calling is the single most important thing to internalize: definition creates the function; calling executes it. You can call a function as many times as you want after defining it once, which is the source of its power.

Functions serve two fundamental purposes: abstraction and modularity. Abstraction means hiding complexity behind a name. Instead of writing ten lines of code every time you need to calculate a tax amount, you write a function called `calculate_tax` and call it by name. The caller doesn't need to know how the calculation works — just what to pass in and what comes back. This makes code read like a description of what it does rather than how it does it. Modularity means breaking a program into independent pieces, each responsible for one task. A function that calculates tax doesn't also format output or read user input — it does one thing. This makes functions easier to test, debug, and reuse. If your tax calculation has a bug, you fix it in one place, and every call site benefits.

The flow of execution when a function is called follows a specific pattern: the program pauses at the call site, jumps to the function body, executes the code inside, and then returns to exactly where it left off. You can trace this by imagining a bookmark: the program puts a bookmark at the call, runs the function, and then picks up where the bookmark is. This is how the call stack works under the hood — each function call adds a frame, and when the function completes, its frame is removed and execution returns to the caller. Understanding this flow is critical for reasoning about program behavior, especially as you move on to topics like parameters, return values, and eventually recursion, all of which build directly on this foundation.

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

Longest path: 74 steps · 323 total prerequisite topics

Prerequisites (3)

Leads To (10)