Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Function (Programming)

A named, reusable block of code that performs a specific task, taking input (parameters) and optionally returning output (a return value).


Quick Answer

A named, reusable block of code that performs a specific task, taking input (parameters) and optionally returning output (a return value). Use a function when the same job appears more than once, or when a long program needs to be split into named pieces. You define it once, for example area(w, h) that returns w * h, and then call it wherever you need it: area(3, 4) gives 12 and area(10, 2) gives 20. Defining a function only describes the job; nothing runs until it is called.

Why This Matters

Functions let you write a piece of logic once, test it once, and use it a hundred times, and a fix inside the function fixes every place that calls it. They also make programs readable, because a line like total = addTax(price) says what happens without showing how. Without them the same code gets copied into many places, and a bug fixed in one copy survives in the others.

Intuitive Explanation

Think of a function as a small machine with a name. def double(n): return n * 2 builds a machine called double that takes a number in and sends twice that number out. Writing the definition does not run it, just as buying a blender does not make a smoothie. The machine runs only when a line calls it: double(7) sends 7 in, the body works out 7 * 2, and 14 comes back to the spot where the call was written.

A call can go anywhere a value can. y = double(5) + 1 runs double first, gets 10, then adds 1, so y is 11. double(double(3)) runs the inner call first, giving 6, then doubles that, giving 12. After each call finishes, the program carries on from exactly where it left off.

A function is not the same as an algorithm, though it usually contains one. The algorithm is the method, such as the steps for finding the largest number in a list; the function is that method packaged under a name, with inputs and an output, so it can be reused. Printing is also not the same as returning: a function that prints its answer shows it on the screen, but the calling code receives nothing it can store or add to.

Think of it like this. A function is like a recipe card filed under a name. Writing the card cooks nothing; cooking happens only when someone pulls the card out and follows it (the call), using today's ingredients (the arguments), and hands over the finished dish (the return value). The same card works every day, with different ingredients each time.

Core idea. Write once, use many times. Functions take input (parameters) and give output (return value).


When to Use

Use a function when the same steps are repeated in several places, when a job deserves its own name (such as calculateTax or drawPlayer), or when you must trace a call and work out its return value. Follow the call into the body, run it with the given inputs, and carry the returned value back. If the question is only about the placeholder names in the definition, focus on Parameters; if it is only about the steps of a method, it is an Algorithm.

Tip. Ask: Is a named block written once and then run by calling it, maybe with inputs and a value sent back?

How to Recognize It

Before deciding a function is the answer, look for a job that is written once under a name and used from other places in the program.

  1. Does the same job appear in two or more places in the program?

    Yes is a strong reason to write it once as a function and call it. If it happens only once, a function can still help by giving the job a clear name.

  2. Does the job need different input values each time it is used?

    Yes means the function needs Parameters to receive them. No means a function with empty brackets will do.

  3. Does the code that uses the job need an answer back from it?

    Yes means the function should send it back with a return statement; see Return Values. No means it can simply do something, like draw or print.

  4. Is the question about the plan of steps, whether or not it is written as code?

    Yes points to Algorithm. A function is one way to package an algorithm in a program, under a name you can call.

Function (Programming) vs Algorithm vs Parameters vs Return Values

A function holds an algorithm, takes parameters and hands back a return value, so all four turn up in one line of code. Ask whether you mean the whole named block or one part of what flows in and out.

Function (Programming)

Meaning
A function is a named block of code that is defined once and runs each time it is called.
Key test
It has a name, a definition, and calls that run it.
Formula
f:D→R
Example
def area(w, h): return w * h, then area(3, 5) gives 15.

Algorithm

Meaning
An algorithm is the step-by-step method itself, which can be written in English, as a flowchart or inside a function.
Key test
The method, whether or not it is code or has a name.
Formula
output=f(input)
Example
To find an average, add all the numbers, then divide by how many there are.

Parameters

Meaning
Parameters are the names in a function's definition that stand for the values passed in.
Key test
The placeholder names inside the definition's brackets.
Example
In def area(w, h), the names w and h are the parameters.

Return Values

Meaning
A return value is the result a function sends back to the line that called it.
Key test
What the call turns into after the function finishes.
Example
area(3, 5) is replaced by its return value, 15.

Formula & Notation

Worked Examples

Example 1 — Trace two calls

Easy

def area(w, h): return w * h. What is the value of total after total = area(3, 4) + area(5, 2)?
Show the steps and the answer
  1. First call: area(3, 4) sets w = 3 and h = 4, and returns 3 * 4 = 12.

    Each call runs the body with its own inputs.

  2. Second call: area(5, 2) sets w = 5 and h = 2, and returns 5 * 2 = 10.

    The same definition is reused with new values; the first call's values are gone.

  3. Add the returned values: total = 12 + 10 = 22.

    Each call is replaced by the value it returns, and then the expression is finished as usual.

Answer: 22

Takeaway: To trace a call, run the body with that call's inputs, then replace the call with what it returns.

Example 2 — Printing is not returning

Standard

Two functions: def triple_a(n): print(n * 3) and def triple_b(n): return n * 3. In Python, what is stored in x and y after x = triple_a(4) and y = triple_b(4)?
Show the steps and the answer
  1. triple_a(4) shows 12 on the screen.

    print sends output to the screen, not back to the calling line.

  2. triple_a has no return statement, so x receives None.

    The calling line gets only what is returned, and nothing was.

  3. triple_b(4) returns 12, so y = 12.

    return hands the value back so it can be stored, added, or passed on.

  4. Check: y + 1 gives 13, but x + 1 causes an error.

    x holds None, which cannot be added to a number.

Answer: x holds None (the 12 was only printed); y holds 12.

Takeaway: Printing shows a value to a person; returning gives it to the program.

Example 3 — Find the mistake: a function that is never called

Application

Pablo writes a program containing only this: def greet(name): print('Hello, ' + name). He runs it, nothing appears, and he decides the computer must be broken. Find the mistake and correct it.
Show the steps and the answer
  1. Look at what the program contains: only the definition of greet.

    A definition tells the computer what greet means; it does not run it.

  2. Look for a call: there is no line greet(...) anywhere.

    The body runs only when the function is called.

  3. Fix: add a call after the definition, such as greet('Mina').

    The call passes 'Mina' in as name and runs the print.

  4. Trace the fix: 'Hello, ' + 'Mina' is joined, so the program prints Hello, Mina.

    The string join happens inside the body, which now finally runs.

Answer: Add a call such as greet('Mina'); the program then prints Hello, Mina.

Takeaway: A function does nothing until it is called, so check that every function you need has a call.

Where You'll See It

  • A blender

    You put fruit in (input), press blend (call it), and get a smoothie out (return value); the blender is a reusable 'function' you use whenever you need that task done.

  • A vending-machine button

    Each labeled button performs one specific job; you 'call' it to get that one snack, like calling a named function to do a specific task.

  • A microwave's popcorn button

    One named button packages a whole set of steps; pressing it (calling it) runs the same popcorn task every time without you redoing the steps.

Common Mistakes

  1. The slip: Defining a function makes it run.

    Why it's tempting: Writing the whole function feels like the work is done and it should act.

    Instead: Defining a function only describes the task; nothing happens until you call (invoke) it by name.

  2. The slip: Parameters and arguments are the same thing.

    Why it's tempting: Both are the values a function works with, so the words feel interchangeable.

    Instead: Parameters are the placeholder names in the definition; arguments are the actual values you pass in when you call the function.

  3. The slip: A good function does as much as possible so you don't need many functions.

    Why it's tempting: One big do-everything function seems efficient and complete.

    Instead: Each function should do one well-defined task; cramming in many jobs makes it hard to reuse, test, and debug.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. `def double(n): return n * 2`. What is `double(7)`? What is `double(double(3))`?

    Hint: For the second one, work from the inside call outward.

  2. `def area(w, h): return w * h`. What does `area(4, 5) + area(2, 3)` give?

    Hint: Each call runs the function separately.

  3. `def greet(name): print("Hi " + name)`, followed by `greet("Ana")` and `greet("Ben")`. How many lines are printed, and what are they?

    Hint: The function body runs once for each call.

  4. `def f(x): return x * x + 1`. Find `f(0)`, `f(3)` and `f(-2)`.

    Hint: Replace `x` with each value and do the multiplication first.

  5. A program works out a 10% tip in five places, each time typing `bill * 0.10`. Should you write a function, or keep repeating the line?

    Hint: What happens if the tip rate changes to 15%?

  6. A student writes `def say_hello(): print("Hello")` and runs the program, but nothing appears. What is wrong?

    Hint: Defining a function and running it are two different steps.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

What is the difference between printing a value and returning it?

Printing shows a value on the screen for a person to read; the rest of the program cannot use it. Returning hands the value back to the code that made the call, so it can be stored or used in more calculations. If def add(a, b) only prints a + b, then total = add(2, 3) shows 5 on screen but leaves total empty, which Python calls None.

Can a function call another function?

Yes, and most real programs are built this way. A function draw_house might call draw_square for the walls and draw_triangle for the roof. Each small function does one job, and the bigger one joins them together. A function can even call itself, which is called recursion, but it then needs a stopping case so the calls do not go on forever.

Are functions in programming the same as functions in maths?

They are related. A maths function such as f(x)=2x+1f(x) = 2x + 1 always gives the same output for the same input and does nothing else. A programming function can work like that, but it can also print, play a sound or change a saved score, and some return nothing at all. Functions that behave like maths ones are the easiest to test.

What are built-in functions?

Functions that come with the language, so you can call them without writing them yourself. In Python, print shows text, len tells you how many items a list has, and round rounds a number. Someone wrote each of them as an ordinary function, with parameters and often a return value, and you call them exactly as you call your own.

Learning Path

Function (Programming)

You are here

You need Algorithm (a job written as exact steps) and Abstraction (hiding detail behind a simple name). A function combines the two: it packages an algorithm under a name, with inputs and an output, so it can be called anywhere. Next, Parameters explains the inputs, Return Values explains the output that comes back, and Scope explains which variables a function can see and which stay private inside it.

See Also