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

Ask yourself: Are the same steps needed in more than one place or with different inputs, so they should be written once under a name?

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.

A vending machine with a name on the front works like a function. You press a button with your choice (the argument), the same machinery inside does the work every time, and an item drops out (the return value); you never need to see inside to use it. The comparison stops at functions that only do a job, such as drawing a shape, and hand back nothing useful.

Formal view

A function f is defined by its signature f(p1,p2,…,pn)→R, its body (a sequence of statements), and its return value. A function call f(a1,a2,…,an) 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)`?
Show solution

Answer

120

First step

1.
This computes 5!=1⋅1⋅2⋅3⋅4⋅5.

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

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

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
  • Writing functions that are too long and do too many things, making them hard to test and reuse
  • Confusing parameters (placeholders in the definition) with arguments (actual values passed at the call site)

Common mistakes guide

If this formula keeps breaking in real problems, read the most common errors before you practice again.

Why this formula matters

Functions let you build a program from pieces you can name, test and trust one at a time. A game might have move_player, check_collision and update_score, each a few lines long, instead of one 500-line tangle. Without them the same steps get copied around, and a bug fixed in one copy survives in all the others.

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?

Write a function when the same steps appear, or will appear, in more than one place, when a task has a clear name like calculate_tax or draw_star, or when a long program should be split into pieces you can test separately. Signals include the word reuse and phrases like 'do this for any input' or 'the same calculation again'. If the steps repeat many times in a row in one spot, a loop is enough. If code should run only when a user acts, attach a function as an event handler.

Why is the Function formula important in CS Thinking?

Functions let you build a program from pieces you can name, test and trust one at a time. A game might have move_player, check_collision and update_score, each a few lines long, instead of one 500-line tangle. Without them the same steps get copied around, and a bug fixed in one copy survives in all the others.

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.