3.1.1.10 Subroutines (procedures/functions) Flashcards
Define a subroutine.
A subroutine is a block of code which only runs when it is called.
How is data passed in subroutines?
You can pass data, known as parameters, into a method.
Define a procedure.
A subroutine that performs a specific task. When the task is complete, the subroutine ends and the main program continues where it left off.
What are the two types of subroutines?
- Procedures.
- Functions.
Outline the advantages of subroutines.
- Making programs shorter, easier to read and understand through breaking down program code into smaller sections.
- You can test procedures or functions separately rather than having to test the whole program, making programs easier to debug.
- If something needs to be changed in a subroutine, it only needs to be changed once, within the subroutine code. This change will then take effect wherever in the program the subroutine is used.
Define a function.
A function works in the same way a procedure does, except it manipulates data and returns a result back from the main program.
Define the syntax for running a subroutine.
E.g. a subroutine named ‘mySubroutine’.
Syntax:
mySubroutine( );
Outline the uses of subroutines.
To reuse code: define the code once and use it many times.