OUCC Tutorials

Blockly Coding Tasks

Functions, Procedures and Subroutines
It is important to realise that Functions and Procedures are defined differently in different programming languages. In Blockly a function can:

  • run some code when called,
  • run some code when called with arguments passed to it
  • run some code when called, with or without arguments passed to it, and return some output.

Variables, Parameters and Arguments
Variables are areas of memory that values can be assigned to and are referred to by an identifier (name). Parameters are variables in a function that are defined when the function is written. The values for these parameters have to be passed to the function when the function is called. Arguments are the values passed to the parameters when a function is called. e.g. in the Blockly code below the convert function expects to be sent the temp in Fahrenheit parameter and returns the temp in Celcius variable. The Execute block calls convert and sends the argument 68 to the function.
Function Example

In Blockly, all variables are global, that is, they can be changed inside or outside of a function. It is not recommended that they are initialised outside the function because this is not good practice for what should be local variables, it is unnecessary and, in certain circumstances, in these question types, can cause marking errors.

Declaration, Initialisation and Assignment
When you, or the task author, creates a new variable its identifier block becomes available in the Variable blocks menu – it has been declared but no value has been assigned to it. Initialisation is the process of assigning an initial value to a variable. Creating a new variable in Blockly does not do this.

When a function is created in Blockly and parameters defined for it, this declares new variables in the Variable blocks menu so that the programmer can use the parameters in the function.

Any variable can be used as a return value. If the question writer adds one to the end of the function block, this will be available in the Variable blocks menu as a declared variable with no value defined.

Note carefully:

  • The most common errors seen in these types of tasks are those where it is assumed that a value has been assigned to a variable when it has not.
  • Initialising function parameter variables outside of a function may well give the correct output for your test data but it will often be marked as incorrect by the grader program.
  • You should not initialise the return variable or the function parameters outside of the function.

<< Back | Next >>

Coding tasks - Intro
Recommended practice
Functions, Procedures and Subroutines

Example 1
Example 2
Example 3
Example 4

Tutorials index

[ tutorials | terms | privacy | info@oucc.uk ]

UKCT Challenges are proud to use The Cuttle Platform to host our challenges.
The Cuttle system can be used for maths and science competitions, entrance exams or employee exams.