This section provides some tutorials that are specific to solving the tasks found in this Challenge.
This type of task is very flexible: Task authors can set problems where a variety of solutions can be given credit for producing the correct answer. These tasks use Function blocks and take this form:
How to answer Blockly Coding tasks in the TCS OCC Challenges:
Write all of your code in the function. There is no need to write any code outside of the function. The task author will have provided the necessary parameters and some suitable test data to pass to the function. The Execute block passes these values to the function block as it calls it. (It is OK to change these values to provide yourself with new tests.)
It is not necessary to understand how to create functions to answer these tasks. Your code needs to form a mini-program inside the function block provided. However, there are a few things that you do need to know:
The Execute Block
The Execute block is made especially for these challenges and performs these tasks:
The grader works by trying input/output pairs to work out whether to mark the tasks as correct or incorrect. The test data, given in the task, forms one such input/output pair but there are also hidden input/output pairs entered into the system by the task author. To be marked as correct, a solution has to have all input/output pairs evaluate correctly. In other words your function has to work as asked for, not just work with the test data.
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:
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 above 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.
In Blockly, all variables are global, that is, they can be changed inside or outside 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, 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 de 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.
This will be marked as correct, however, it is not recommended that you add any code outside of the function. In these sort of tasks, it is intended that all of your programming should be completed inside the function.
This will be marked as wrong because the parameter is assigned the value 68 when the function is called but then reset to 0 in the first line of code in the function.
This will be marked as correct and follows recommended practice.
This will give the correct output but be marked as wrong!
(This is due to the way the marking program has saved the temp in Celsius variable outside the function and then the function returns another value.)