Function Formula

Function is a named, reusable block of code that performs a specific task and can optionally accept inputs (parameters) and return a result.

The Formula

def function_name(parameters): โ†’ body โ†’ return value

When to use: Like a recipe: you name it, write it once, and call it whenever you need it.

Quick Example

def square(x): return x * x โ€” calling square(5) returns 25 without rewriting the logic.

What This Formula Means

A named, reusable block of code that performs a specific task and can optionally accept inputs (parameters) and return a result. Functions allow you to organize code into logical units, write a solution once, and invoke it from anywhere in the program.

Like a recipe: you name it, write it once, and call it whenever you need it.

Formal View

A function ff is defined by its signature f(p1,p2,โ€ฆ,pn)โ†’Rf(p_1, p_2, \ldots, p_n) \to R, its body (a sequence of statements), and its return value. A function call f(a1,a2,โ€ฆ,an)f(a_1, a_2, \ldots, a_n) binds arguments to parameters and evaluates the body.

Worked Examples

Example 1

medium
Trace: `def f(n): total=1; for i in range(1, n+1): total*=i; return total`. What is `f(5)`?

Answer

120120

First step

1
This computes 5!=1โ‹…1โ‹…2โ‹…3โ‹…4โ‹…55! = 1 \cdot 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

medium
Trace keyword args: `def f(a, b=2, c=3): return a + b + c` called as `f(1, c=10)`. What is the result?

Example 3

hard
Trace recursion: `def gcd(a, b): return a if b==0 else gcd(b, a%b)`. What is `gcd(48, 18)`?

Common Mistakes

  • Defining a function but never calling it, then wondering why nothing happens - Fix this by naming the input, process, output, evidence, and checking "Am I tracing how values change and how control moves through the program from input to output?" before using the concept.
  • Writing functions that are too long and do too many things, making them hard to test and reuse - Fix this by naming the input, process, output, evidence, and checking "Am I tracing how values change and how control moves through the program from input to output?" before using the concept.
  • Confusing parameters (placeholders in the definition) with arguments (actual values passed at the call site) - Fix this by naming the input, process, output, evidence, and checking "Am I tracing how values change and how control moves through the program from input to output?" before using the concept.
  • Using function from a keyword alone - Signal words like variable, value, condition only point to a possible model; the computing structure must match too.

Common Mistakes Guide

If this formula feels simple in isolation but keeps breaking during real problems, review the most common errors before you practice again.

Why This Formula Matters

Functions are the primary abstraction in programming; nearly all software is organized around them. They enable code reuse, improve readability, simplify testing, and allow teams to divide work by assigning different functions to different developers.

Frequently Asked Questions

What is the Function formula?

A named, reusable block of code that performs a specific task and can optionally accept inputs (parameters) and return a result. Functions allow you to organize code into logical units, write a solution once, and invoke it from anywhere in the program.

How do you use the Function formula?

Like a recipe: you name it, write it once, and call it whenever you need it.

Why is the Function formula important in CS Thinking?

Functions are the primary abstraction in programming; nearly all software is organized around them. They enable code reuse, improve readability, simplify testing, and allow teams to divide work by assigning different functions to different developers.

What do students get wrong about Function?

Defining a function doesn't run it โ€” you must call it. Parameters (defined) differ from arguments (passed in).

What should I learn before the Function formula?

Before studying the Function formula, you should understand: function programming, return values, modular design.