Functions and Procedures in Pseudocode

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 - 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:

  • A subroutine is a named as an 'out of line' block of code that may be executed/called by simply writing its name in a program statement.
  • As your programs become larger and more complex, they need to be broken down into smaller, self-contained sections.
  • We can set values in a function or any sub procedure by using parameters in the opening of that procedure. For example "Function convUser(a=23)".

Advantages:

  • It makes code a lot neater, so instead of having a clutter of functions, variables, and code you have it in a subroutine.
  • This makes the program run a lot more efficiently, using less memory and processer power.
  • It can be called and executed whenever the programmer wants it to.
  • Can be used more than once, without having to re-write the code.


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.


Functions can be declared with either the Function keyword, or the Subroutine keyword. As stated by AQA: "Subroutines that contain a RETURN keyword are functions. Those that do not contain a RETURN keyword are procedures". Both terms can be used interchangeably.



Simple Subroutine

Loading...

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


Parameters

Loading...

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 Subroutine

Loading...

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.

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 and try out some of your new skills!


Read Our Guide on
Variables
in Pseudocode



© 2023 PseudoEditor.com | All rights reserved

Useful Links

Sign InSign UpFeaturesFAQ