Functions and Procedures

Last Updated on

Although it may seem simple, functions and procedures in pseudocode can be quite complex. That is why we have made this quick guide for you to follow! Like all the guides on PseudoEditor, we follow the pseudocode standards of AQA and Edexcel – the main exam boards in the UK - you should read some edexcel past papers, these standards are universal and provide pseudocode that can be read by every programmer!

So, with most programs you write, subroutines and functions can be an essential part of them, with them allowing certain parts of code to be called multiple times, allowing efficiency and ease of access in terms of writing programs easily. What makes functions so useful? It is simple, imagine you had to write the same piece of code out 5 times, or you could just write it once and wrap it in a function, then call it whenever it is needed. That is why procedures and functions are essential in every programming language, not just pseudocode.

Code Icon

Basics:

Advantages:

How to write Functions in Pseudocode

No standard for pseudocode syntax exists. However, there are some commonly followed ways to help make pseudocode written by one programmer easily understood by another programmer, that applies with functions. We have outlined the best ways to write functions in pseudocode with examples.

Simple Function

Function addNums()
    unum1 = 10
    unum2 = 20
    result = unum1 + unum2 
    Return result 
Endfunction

This is a lot easier than it seems to write, you just start with the opening of the function followed by the end function. Inside that you write the code for whatever you want your function to do, whether that is adding two digits or ordering an array. But remember functions have to return a value always, if you do not want your procedure to return a value, then use a subroutine! Functions can return Boolean values, integers, strings… literally every data type.

Parameters

Function addNums(unum1, unum2)
    result = unum1 + unum2
    Return result
Endfunction

A very helpful thing in pseudocode and most high-level programming languages is the ability to pass parameters into a function or subroutine. This can be very helpful as you can then use values from the main program in your function, with the ability to then return a new value, whether that is a sum of two numbers passed in like the example above, or something completely different. This can be great for large programs!

The best bit about pseudocode is you do not have to worry about syntax. This allows us to just have the variable names in brackets, this displays we are passing in parameters, then we can use them same variable names to present the values inside of the function. It is that simple. It is a lot easier than it might sound, you just pass in values and use them!

Calling a Function

addNums(optional, parameters)

Calling a function just requires you to reference the function name and then the parameters inside of parenthesis. These will be the values/variables that you will pass not the function. In this case, we are calling addNums.

Subroutines in Pseudocode

Some parts of subroutines can easily be mistaken with parts of functions, we get it. Pseudocode can be complicated sometimes, so we are here to help, we have outlined the 3 major parts to subroutines in pseudocode.

Differences:

Basic Subroutine

Sub divNums()
    unum1 = 50
    unum2 = 10
    result = unum1 / unum2 
    Return result 
Endsub

Subroutines are incredibly similar to functions, however when declaring a subroutine in pseudocode, be sure to put “Sub” and then your subroutine name, parameters, blah blah. Then from there you can write the rest of your code inside the subroutine, like we have, but ensure you add an Endsub to show that the subroutine has ended!

Parameters

Sub divNums(unum1, unum2)
    result = unum1 / unum2 
    Return result 
Endsub

Once again, this is just like functions. When you are passing your parameters in you must ensure that the name of the parameter is the exact name that you use when mutating that value. Otherwise, you are creating a completely new variable!

Calling a Subroutine

divNums(optional, parameters)

Yep, you guessed it! Just the same as a function. However, depending on how you like to do it, you can also use Call divNums() to call the subroutine. This can sometimes make it clearer you are calling a subroutine!

Code Icon

Yep, it is that simple! You now know exactly how to use, call, and declare subroutines and functions in pseudocode! To have a go yourself, be sure to head to our online pseudocode editor, here and try out some of your new skills!