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
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
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
Worked examples
Example 1
mediumShow solution
Answer
First step
See the full worked solution + why-it-works coaching
SetupKey insightWhy it worksCommon pitfallConnection
Example 2
mediumExample 3
hardCommon 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.