A named block of code that does one job, which you can call from anywhere in the program instead of rewriting it.
A function is a named block of code that does one job, which you can run from anywhere just by using its name.
Defining a function writes the instructions down, and calling it is what makes them actually run.
In Python you define with def flash_led(): and call with flash_led(), where the brackets on the second line are what sets it going.
Defining a function and never calling it means nothing happens at all, which is one of the quietest bugs a beginner meets.
A good function does one thing you could name in three words, like "read the sensor" or "show the score".
Example
Python: def flash_led():
pin0.write_digital(1)
sleep(200)
pin0.write_digital(0)
Parameters Let One Function Do Many Jobs
A parameter is a named slot written inside the brackets of the definition.
An argument is the real value you drop into that slot when you call the function.
So def flash_led(times): has one parameter, and flash_led(3) passes the argument 3 into it.
Parameters are what stop you writing flash_once, flash_twice and flash_three_times as three near identical functions.
Order matters, so a function defined as beep(pitch, length) and called as beep(200, 500) sets pitch to 200 and length to 500.
Name parameters as carefully as variables, since pause(ms) tells the reader the unit and pause(x) tells them nothing.
Return Values Hand an Answer Back
return sends a value back to whatever called the function, and the call gets replaced by that value.
If def average(a, b): return (a + b) / 2, then result = average(4, 6) puts 5.0 into result.
A function with no return still works fine, which suits anything whose job is to act rather than to answer.
return also stops the function on the spot, so any line written after it never runs.
Keep functions that answer questions separate from functions that do things, because one that does both is far harder to test.
Common Mistake
Printing a value and returning a value are different jobs, since print puts it on screen and return gives it to the rest of the program.
A function that only prints its answer cannot have that answer used in a later calculation.
Scope: Where a Variable Can Be Seen
A variable created inside a function is local, and it disappears the moment that function finishes.
A variable created outside every function is global, and any function in the program can read it.
Local scope is useful rather than annoying, since two functions can each use a counter called i without tripping over each other.
Changing a global from inside a Python function needs the word global on a line of its own first, which is usually a hint that a parameter would be tidier.
Bugs where a value seems to reset itself are almost always a local variable sharing a name with a global one.
Hint
Search your file for a line you think you have typed twice, because that is the fastest way to find a function you should have written.
If the copy and the original have drifted apart, work out which one is right before you combine them.
Repeated Code Is a Function Waiting to Happen
Copy three lines and paste them elsewhere and you now have two places to fix when the logic turns out wrong.
Turn a repeated block into a function the second time you need it, rather than waiting until the fifth.
A function also puts a name on the block, which explains the code better than a comment sitting above it.
Shorter main code is easier to check against your flowchart, because one call lines up with one box.
Having one place to change is what makes a design testable, since you can retune the buzzer once and hear the new tone everywhere.
Exam technique
Screenshot your code with the function definitions visible, since a wall of main code shows a moderator no structure.
Name each function in your design folder next to the design specification point it satisfies.
A short table of function name, what goes in and what comes out is strong evidence for Creating the solution.
Building a Program From Modules
A module is a group of related functions kept together, such as everything that touches the display.
A typical micro:bit project splits into three modules: read the inputs, decide what to do, drive the outputs.
The main program then reads as five or six calls in a row, which someone else can follow without reading any of the detail.
Modules can be built and tested one at a time, so a broken display module does not stop you proving the sensor module works.
Group projects split along module lines, since two people can write two modules once they agree what goes in and what comes out.
Agreeing those inputs and outputs before anyone writes code is the step people skip and then spend an afternoon regretting.
Active recall
What is the difference between defining a function and calling it?
In flash_led(3), which part is the parameter and which is the argument?
Why can a printed answer not be used later, while a returned one can?
What happens to a local variable when its function finishes?
Name the three modules a typical sensor project splits into.