What are procedures and functions?
The new release of Microsoft MakeCode is now live and this week we’re examining one of its improved features in depth – procedures and functions - using a brand new micro:bit project.
People often use the terms procedure and function to mean the same thing. They are similar but slightly different.
Procedures are chunks of code that do a lot of work. They let you re-use the same instructions many times in different places in a program. For example in game design, if the same animation appears at different points in the game, using a procedure saves you from having to write out the same code every time you want to show it.
Using procedures in your program makes the code more compact, efficient, easier to read, modify and debug. They're also sometimes called sub-routines.
Functions are also pieces of code that you want to re-use in the same program. They’re usually used for calculations because they are good at processing data, for example calculating formulas with different values. You send data to a function, the function processes it and then it returns the new value.
You could write a function that takes the width and height of a rectangle and returns its area, or one that converts a temperature reading from centigrade to Fahrenheit.
You can use the same function in different places and contexts in your program.
'Function' is a mathematical term describing a relation or expression involving one or more variables, for example (area = width x height). Functions in computer programs work in a similar way, which is why they are good for performing calculations.
In MakeCode functions now behave like ‘real’ functions. You can pass data to them, the function can process the data and return it: it sends the processed data back to the point in the program where you need it. This means you can use functions in the same places you use variables and they become much more flexible.
Procedures in use: tilt alarm
MakeCode calls all reusable sections of code ‘functions’ whether they are actually functions or procedures.
Look at our Tilt alarm project above to understand how procedure-like functions work. Here we use a function called ‘alarm’ which makes it easy to reuse a section of code. It's used twice: to show a visual and audible alert signal both when it’s shaken, and when it receives an alarm radio message from another micro:bit.
Using a procedure means we only need to create the alarm blocks once, and if we decide to edit the code to improve the alarm display and sounds, we only need to change one section of code to make use of the improved alarm feature in two different contexts.
Functions in use: Fahrenheit thermometer
In this new Fahrenheit thermometer project, we’ve made a function called convertCtoF to convert temperature readings from centigrade to Fahrenheit.
When you press button B, the program calls the function from the show number block, using it as if were a variable. This passes the current temperature in centigrade to the function.
The function temporarily stores this number in a variable C, then converts it to Fahrenheit using the unit conversion formula:
Fahrenheit = centigrade x 1.8 + 32
The function then returns it back to the show number block, so your micro:bit can show the temperature in Fahrenheit as well as centigrade on its LED display.
Have a go with functions!
These projects are available in both MakeCode and Python, so try them out for yourself and compare how functions and procedures work in blocks and a text-based language.
Experiment with creating your own functions that allow you easily to re-use code in different contexts within your program.
Recap: why functions are useful
Making your code more compact
In the examples above, using functions saved us from having to add the same blocks twice, making our programs shorter and more efficient.
Making your code easier to read
By breaking down repetitive and complex tasks and moving them into functions, you make it easier for someone else to understand your code – or for you if you come back to look at your code a long time after you wrote it! That's why it’s important to give functions meaningful and descriptive names.
Making your code easier to fix and improve
Code that is easier to read is easier to debug. It makes it easier to isolate different parts of your program for testing.
Making your code easier to re-use
If you make a useful function it’s easy to use it in other similar projects, for example a really cool animation, a set of sounds or a complex unit conversion.