Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide and must not be used by students during a NEA where internet access is not allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Functions / Procedures

When writing a program it's often really useful to break down the problem you're trying to solve into smaller parts. This process is called decomposition and it helps you to solve each small part of the problem separately which can lead to more reliable and efficient code.

Choosing how to break up your program into smaller parts is one of the most important design decisions.

Anything your program has to calculate can be turned into a function. Any task that your program might have to repeat can be turned into a procedure.

Top tips:

  • Functions and procedures both have names, which make your code easier to understand.
    Choose names that describe what the function or procedure does. e.g. MyFunction1() is not nearly as helpful as CalculateAverage()
  • Both functions and procedures are sections of code that can be repeated.
    You need to define a function / procedure once by writing out the lines of code that the function / procedure will follow when it runs. After you've defined it you can call it (make it run) as many times as you like just using its name.
  • Both functions and procedures can have parameters which can customise what they do and how they work.
    Parameters work as variables that only exist inside the function or procedure. You can pass data to the function / procedure whenever you call it through one of these parameters.
  • The only difference between functions and procedures is that functions return a value but procedures don't.
    A return value is the result of a calculation that gets passed back to the part of the code that called the function so that it can be saved or used later in the program.
Create and use a procedure
   

A procedure is a section of code that has a name that you can re-use to do something useful.

A procedure definition is the code that tells the computer what to do whenever that procedure runs.

The procedure wont actually do anything until you call it.

This example defines a procedure that shows a random word then calls it twice.

# define ShowRandomWord procedure PROCEDURE ShowRandomWord() BEGIN PROCEDURE SET words TO ["wibble", "wobble", "plop"] INTEGER i SET i TO RANDOM(2) SEND words[i] T DISPLAY END PROCEDURE # Call ShowRandomWord twice ShowRandomWord() ShowRandomWord()
' define ShowRandomWord procedure Sub ShowRandomWord() dim words As String() words = {"wibble", "wobble", "plop"} dim i as Integer i = Rnd() * 2 Msgbox(words(i)) End Sub ' Call ShowRandomWord twice ShowRandomWord() ShowRandomWord()
import random # define show_random_word procedure def show_random_word(): words = ["wibble", "wobble", "plop"] print(random.choice(words)) # call show_random_words twice show_random_word() show_random_word()
// this code assumes there is a function defined called random(min, max) // which chooses a random number between a minimum and maximum (inclusive) // this is not in the pseudocode spec // define show_random_word procedure procedure show_random_word() switch random(1, 3) case 1: print("wibble") case 2: print("wobble") case 3: print("plop") endswitch endprocedure // call show_random_words twice show_random_word() show_random_word()
{ this code assumes there is a function defined called Random(min, max) } { which chooses a random number between a minimum and maximum (inclusive) } { this is not in the pseudocode spec } { define ShowRandomWord procedure Declare ShowRandomWord i is integer set i = Random(1, 3) if i == 1 output "wibble" end if if i == 2 output "wobble" end if if i == 3 output "plop" end if End Subroutine // call ShowRandomWord twice call ShowRandomWord call ShowRandomWord
Create and use a procedure with parameters
   

A procedure is a section of code that has a name, that you can re-use to do something useful.

Parameters are variables that allow you to send data to the procedure to customise what it does or how it works. 

A procedure definition is the code that tells the computer what to do whenever that procedure runs. The parameters are the variables named in brackets after the procedure name.

The procedure wont actually do anything until you call it.

This example defines a procedure which will display a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.  

This procedure is called to show a random number between 10 and 20 and then called again to choose a random number between 50 and 100.

# Define the procedure PROCEDURE ShowRandomBetween(min, max) BEGIN PROCEDURE INTEGER n SET n TO RANDOM(max - min) + min SEND n TO DISPLAY END PROCEDURE # Call the procedure ShowRandomBetween(10, 20) ShowRandomBetween(50, 100)
' Define the procedure Sub ShowRandomBetween(min As Integer, max As Integer) dim n As Integer n = (Rnd() * (max - min)) + min MsgBox(n) End Sub ' Call the procedure ShowRandomBetween(10, 20) ShowRandomBetween(50, 100)
import random # define the procedure def show_random_between(min, max): print(random.randint(min, max)) # call the procedure show_random_between(10, 20) show_random_between(50, 100)
// this code assumes there is a function defined called random(min, max) // which chooses a random number between a minimum and maximum (inclusive) // this is not in the pseudocode spec // define the procedure prcedure show_random_between(min, max): print(random(min, max)) endprocedure // call the procedure show_random_between(10, 20) show_random_between(50, 100)
{ not specified in eduqas pseudocode reference }
Create and use a function
   

A function is like a procedure except that it always returns a value.

This return value is calculated when the function is called and can be saved into a variable or used later in the program.

Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.

This example defines a function which chooses a random word. This function is called twice with the return value being stored into separate variables.

# define ChooseRandomWord function FUNCTION ChooseRandomWord() BEGIN FUNCTION SET words TO ["wibble", "wobble", "plop"] INTEGER i SET i TO RANDOM(2) RETURN i END FUNCTION # Call ChooseRandomWord twice STRING FirstWord SET FirstWord TO ChooseRandomWord() STRING SecondWord SET SecondWord TO ChooseRandomWord()
' define ShowRandomWord function Function ShowRandomWord() dim words As String() words = {"wibble", "wobble", "plop"} dim i As Integer i = Rnd() * 2 Return words(i) End Function ' Call ShowRandomWord twice Dim FirstWord As String FirstWord = ShowRandomWord() Dim SecondWord As String SecondWord = ShowRandomWord()
import random # define choose_random_word function def choose_random_word(): words = ["wibble", "wobble", "plop"] return random.choice(words) # call choose_random_words twice first_word = choose_random_word() second_word = choose_random_word()
// this code assumes there is a function defined called random(min, max) // which chooses a random number between a minimum and maximum (inclusive) // this is not in the pseudocode spec // define choose_random_word function function choose_random_word() switch random(1, 3) case 1: return "wibble" case 2: return "wobble" case 3: return "plop" endswitch endfunction // call choose_random_words twice first_word = choose_random_word() second_word = choose_random_word()
{ not specified in eduqas pseudocode reference }
Create and use a function with parameters
   

A function is like a procedure except that it always returns a value.

Parameters are variables that allow you to send data to the function to customise what it does or how it works. 

The parameters are the variables named in brackets after the function name.

This return value is calculated when the function is called and can be saved into a variable or used later in the program.

Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.

This example defines a function which will generate and return a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.  

This function is called to choose a random number between 10 and 20 and then called again to choose a random number between 50 and 100. Both numbers are returned and saved into separate variables.

# Define the function FUNCTION ChooseRandomBetween(min, max) BEGIN FUNCTION INTEGER n SET n TO RANDOM(max - min) + min RETURN n END FUNCTION # Call the function INTEGER FirstNumber SET FirstNumber = ChooseRandomBetween(10, 20) INTEGER SecondNumber SET SecondNumber TO ChooseRandomBetween(50, 100)
' Define the function Sub ShowRandomBetween(min As Integer, max As Integer) dim n As Integer n = (Rnd() * (max - min)) + min Return n End Function ' Call the function Dim FirstNumber As Integer FirstNumber = ShowRandomBetween(10, 20) Dim SecondNumber As Integer SecondNumber = ShowRandomBetween(50, 100)
import random # define the function def choose_random_between(min, max): print(random.randint(min, max)) # call the function first_number = choose_random_between(10, 20) second_number = choose_random_between(50, 100)
// this code assumes there is a function defined called random(min, max) // which chooses a random number between a minimum and maximum (inclusive) // this is not in the pseudocode spec // define the function function choose_random_between(min, max): return random(min, max) endfunction // call the function first_number = choose_random_between(10, 20) second_number = choose_random_between(50, 100)
{ not specified in eduqas pseudocode reference }

Loading...