Skip to content
BENIBIX Learn

Function

Development🧩 Programming ConceptsFunctions

Also known as Subroutine, Procedure

A named block of code that does one job. Write it once, use it many times.

Think of it like

Think of it like a toaster. Bread goes in, you press one button, and toast comes out.

Example

A shop writes one function, add_tax, that adds 20% to a price. Every page that shows a price calls it.

Why it matters

Functions stop you writing the same code twice. Fix a bug in one function and every place that uses it is fixed.

Definition

A function is a named block of code that does one job. You write it once and then use it as often as you like. It can take values in, called parameters, and hand a result back. Calling it by name runs the code inside. Splitting a program into small functions makes it easier to read, test and fix.

Example code

python
def greet(name):
    return "Hi, " + name + "!"

print(greet("Emma"))   # Hi, Emma!
The job is written once, under a name, then used with one line.

Common mistake

Writing one huge function that does ten jobs. Small functions with clear names are far easier to follow.

Key takeaways

  1. 1
    One job. A good function does one clear thing.
  2. 2
    Write once. Use it from as many places as you like.
  3. 3
    In and out. Values go in; a result comes back.

Compare

Learn it properly