GCSE Computer Science, Unit 2, Lesson 4 of 6
Brainlag Plus

Subroutines

GCSE Computer Science, Programming in exam style, 20 minutes

Have Plus? Sign in

Functions and procedures, parameters and return values, local and global variables, and why programs are built from subroutines.

Functions and procedures

A subroutine is a named block of code that runs when it is called.

  • A function returns a value to the code that called it.
  • A procedure does a job but returns nothing. (AQA style calls both SUBROUTINE; Python uses def for both.)
  • Parameters are the names in the brackets of the definition; the values passed in when it is called are arguments.
function area(w, h)
    return w * h
endfunction
procedure banner(msg)
    print("** " + msg + " **")
endprocedure
banner("Room " + str(area(3, 4)))
SUBROUTINE area(w, h)
    RETURN w * h
ENDSUBROUTINE
SUBROUTINE banner(msg)
    OUTPUT "** " + msg + " **"
ENDSUBROUTINE
banner("Room " + INT_TO_STRING(area(3, 4)))
def area(w, h):
    return w * h
def banner(msg):
    print("** " + msg + " **")
banner("Room " + str(area(3, 4)))

The rest of this lesson and its 7 practice questions are in Plus.

Screen 1 of 3, free to read.

  • 7 exam-style questions, marked as you go, each with a worked solution
  • Anything you get wrong is saved to your flashcards