Note that your final mark will not be saved in the system.
Data Structures / Structured Programming GapFill
You must fill all the gaps before clicking ‘Check Answers!’
One of the key skills of a good programmer is the ability to break a problem down into smaller sections so that each section becomes easier to solve. This means that different sections of the whole solution can be completed by different people and these separate modules are much easier to and correct before they are put together in the whole solution. In addition, as the modules of code are they can be re-used in different parts of the solution, for example: opening or saving a file.
This approach to programming can be achieved through the use of . This is a block of code which is defined outside the main body of the program, when a is made in the main program to the subroutine, control of the program passes temporarily to the subroutine until it has been completed, when it returns to the main program. Each subroutine is given its own which is used in the main program to ‘call’ the subroutine which is always followed a set of brackets.
Examples:
SUBROUTINE multiply_nums(a,b) total = a * b OUTPUT total ENDSUBROUTINE
SUBROUTINE get_nums(t) OUTPUT 'Enter your', t, 'number' num ← USERINPUT RETURN num ENDSUBROUTINE
a = get_nums('first') b = get_nums('second') multiply_nums(a,b) | SUBROUTINE calc_circle_a() OUTPUT 'Enter circle radius' r ← USERINPUT PI ← 3.1459 area ← PI * r**2 RETURN area ENDSUBROUTINE calc_circle_a() |
When a subroutine needs data in order to work this is shown as inside the , when the subroutine is designed these are just placeholders with the actual values being supplied when the subroutine is called. Subroutines can also data back into the main program for use in other subroutines and so pass data between subroutines. Subroutines that are designed to RETURN values are called ; subroutines that do not return a value are called .
It is good programming practice to use variables wherever possible, the variables used in the example ‘calc_circle_a’ above are local variables as they only exist inside the subroutine .They are not part of the main program which cannot access these; for example if a programmer tried to write PRINT(area) in the main program this would cause an error.