KeyStage

Key Stage 3 computing · Programming

KS3 functions and procedures: a guide for parents

A procedure or function is a named block of code that does one job and can be used again and again. In Years 7 to 9, children learn to design programs as modules, building small, tested pieces and putting them together, instead of writing one long list of instructions.

Functions and procedures, explained

As programs grow, writing every step in one long block becomes slow and error-prone. If the same calculation is needed in five places, the code is typed five times, and fixing a mistake means finding and changing all five. The answer is modular design: the program is split into smaller parts, each with a name and a clear purpose, and the main program simply calls them in order. This is the programming version of decomposition, which children have met since primary school.

The curriculum names two kinds of module, procedures and functions. Strictly, a procedure carries out a task, such as drawing a square or printing a menu, while a function works something out and gives the answer back, as a calculator button does. The school chooses the language. In Python, which many schools use and which the example below is written in, both are written the same way with the word def, and the difference is whether a value is returned. Some teachers use the words interchangeably, so do not worry if your child does too.

A function can be given information to work with. These are called parameters when the function is written, and arguments when it is used. A function called sale_price might take the original price as a parameter, so one definition works for a £20 shirt or a £36 coat. A function can also return a value, which the rest of the program can store, print or use in another calculation. Whatever is created inside a function normally stays inside it, which is called local scope.

There are good reasons to work this way. Code can be reused, a bug fixed in one place is fixed everywhere, and each function can be tested on its own with chosen values before it is joined to the rest. Several people can work on different functions of the same program. Pupils are also encouraged to give functions clear names and to keep each one to a single job, so the main program reads almost like a plan in plain English.

Expect Key Stage 3 tasks to start by using functions that already exist, such as len or print, then writing simple ones with no parameters, then adding parameters and return values, and finally designing a whole program from several functions. As with all programming at this stage, the curriculum asks for repeated practical experience, so progress comes from writing many small programs and fixing what goes wrong, not from memorising definitions.

Words your child will hear

Procedure
A named block of code that carries out a task and is used by calling its name.
Function
A named block of code that works something out and usually returns an answer.
Parameter
A variable named in a function’s definition that receives information when the function is used.
Argument
The actual value given to a function when it is called, such as 20 in sale_price(20).
Return value
The result a function hands back to the part of the program that called it.
Local variable
A variable that exists only inside the function that created it.
Modular
Built from separate parts, each with its own job, that can be written and tested one at a time.

What the National Curriculum says

The programme of study for Key Stage 3 says:

  • Pupils should be taught to use 2 or more programming languages, at least one of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables or arrays]; design and develop modular programs that use procedures or functions
  • Aim: pupils can analyse problems in computational terms, and have repeated practical experience of writing computer programs in order to solve such problems.

Objectives from the National Curriculum in England, © Crown copyright, used under the Open Government Licence v3.0.

A worked example

A shop program works out sale prices with 25% off in several places. Rewrite the repeated calculation as a function, then use it for a £20 shirt and a £36 coat.

  1. Spot the repetition. The same calculation, price minus a quarter of the price, appears more than once, with only the starting price changing.
    shirt = 20 - 20 * 0.25
    coat = 36 - 36 * 0.25
  2. Decide what changes and what comes back. The original price changes each time, so it becomes the parameter. The new price is the result, so it is returned. Choose a clear name: sale_price.
  3. Write the function. The word def starts the definition, the indented line is the body and return sends the answer back. Nothing happens yet: this only defines the function.
    def sale_price(original):
        return original - original * 0.25
  4. Call the function with arguments. Python shows 15.0 and 27.0. Check by hand: 25% of 20 is 5, so 15, and 25% of 36 is 9, so 27.
    print(sale_price(20))
    print(sale_price(36))
  5. See the benefit. If the shop changes its mind and offers 30% off, only the number 0.25 inside the function is edited, and every price in the program updates.

sale_price(20) gives 15.0 and sale_price(36) gives 27.0. One tested function now replaces every repeated calculation, so a change is made in only one place.

Common mistakes and how to help

Thinking that writing def makes the function run

Children expect code to run line by line, so a definition seems to do something. It only stores the instructions under a name.

What helps: Compare it to writing a recipe, which is not the same as cooking it. Nothing happens until the function is called by name, as in sale_price(20).

Using print inside a function when it should return the answer

Printing shows the answer on screen, so it looks as if it works. But the rest of the program cannot use a printed value. A function that only prints gives back nothing, so adding it to another number causes an error.

What helps: Ask “Does the function show the answer to a person, or hand it back to the program?”. Try using the function’s result in another calculation and see which version works.

Expecting a variable made inside a function to exist outside it

A variable made in one part of a program seems to belong to the whole program. Inside a function, though, it is local and is forgotten when the function ends.

What helps: Think of a function as a room with its own whiteboard. What is written there is wiped when you leave, so anything needed outside must be returned.

Believing the argument must have the same name as the parameter

Children see original in the definition and assume they must also have a variable called original when calling the function.

What helps: Call it with a number, then with a variable of a different name, and show that both work. The parameter name only matters inside the function.

How to help at home

  • Describe each function in one sentence. Ask your child what each of their functions does. If it needs more than one sentence, it may be doing too many jobs and could be split. A good name tells you the job before you read the code.
  • Find the repeated code. Ask to see a program where the same few lines appear more than once. Challenge them to say what would change each time, which is the parameter, and what would come back, which is the return value.
  • Use a recipe as a model. A recipe with a method that is followed for different amounts is a good model. “Make pancakes for 4” and “make pancakes for 10” use the same method with a different argument. Ask where the parameter would go.
  • Play the named-moves game. Make up a short dance, called a name such as “wave-spin”, and use that name in a longer routine instead of repeating each step. Then add a parameter, such as “wave-spin 3 times”.
  • Test one piece at a time. Ask your child to check a function with three test values before using it in the main program, including an odd one such as 0. Predict each answer on paper first, then run it. Professional programmers test their code in the same way.

More on programming

Everything in Key Stage 3 computing

Updated .