Subroutines: Functions and Procedures

GCSE Computer Science ยท Programming

What is the difference between a function and a procedure?

Both are subroutines: named blocks of code that perform a task and can be called from elsewhere in a program. A function returns a value to the code that called it, so it can be used in an expression. A procedure carries out a task but does not return a value. Subroutines reduce repetition and make code easier to test and maintain.

What subroutines are

A subroutine is a named block of code that performs a specific task and can be called (run) whenever needed. Subroutines come in two kinds: functions and procedures. They make programs shorter, clearer and easier to maintain.

Why use subroutines?

  • Avoid repetition โ€” write the code once, call it many times.
  • Break a big problem into smaller parts (decomposition) โ€” each subroutine solves one task.
  • Easier to read, test and debug โ€” you can test each subroutine separately.
  • Reusable โ€” the same subroutine can be used in other programs.

Functions vs procedures

FunctionProcedure
Returns a value?Yes โ€“ gives a result backNo โ€“ just performs actions
Typical useCalculate something you use laterDo a task (e.g. print a menu)
Example calltotal = add(3, 4)showMenu()
function add(a, b)
   return a + b
endfunction

procedure greet(name)
   output "Hello " + name
endprocedure

Parameters and arguments

  • A parameter is a variable listed in the subroutine definition โ€” the data it needs to do its job (a and b above).
  • An argument is the actual value you pass in when calling it (add(3, 4) passes 3 and 4).
  • Passing data in through parameters makes a subroutine flexible โ€” it works on different values each time.

Return values

A function sends a result back with return. The calling code can store or use it:

answer = add(10, 5)   // answer becomes 15

Local vs global variables

  • A local variable is created inside a subroutine and only exists there โ€” it can't be seen by the rest of the program.
  • A global variable is declared outside all subroutines and can be used anywhere.
  • Prefer local variables: they avoid accidental clashes, keep subroutines self-contained, and free memory when the subroutine ends.

Worked example

What does output add(add(2, 3), 4) print, using the add function above?

1. Inner call: add(2, 3) = 5.

2. Outer call: add(5, 4) = 9. โœ“

Common mistakes

  • Confusing a function (returns a value) with a procedure (doesn't).
  • Mixing up parameter (in the definition) and argument (the value passed in).
  • Trying to use a local variable outside its subroutine.

Exam tips

  • Learn the one-line difference: function returns a value, procedure does not.
  • Be ready to explain two benefits of subroutines (e.g. reuse + easier debugging).
  • Know why local variables are preferred over global ones.

Key facts to remember

  • A subroutine is a named, reusable block of code โ€” a function (returns a value) or a procedure (performs a task).
  • Parameters receive data (the passed-in values are arguments); functions send results back with return.
  • Local variables exist only inside a subroutine; global ones exist everywhere โ€” locals are safer.
Test yourself on Programming

5 quick GCSE Computer Science questions, marked instantly, so you know what stuck. Free account, no card needed.

Start the free quiz โ†’
Don't understand a part?

Sign in and ask our AI tutor to explain any passage in plain English.

Try AI explanations โ†’

More on Programming

Programming Fundamentals Selection and Iteration Arrays, Lists and Records String Manipulation Robust Programming String Manipulation and File Handling

โ† All GCSE Computer Science notes