Subroutines provide an independent section of code that can be called from another routine while the program is running. In this way, subroutines can be called from another routine while the program is running. In this way, subroutines can be used to perform common tasks within a program.
Calling is to activate a subroutine. To do this, you specify the subroutine’s name and, optionally, parameters.
Why use subroutines:
The subroutine can be called when needed (avoiding the need for repeating identical code).
The subroutine can be passed data to use in comparisons or calculations (it can return data to the main program).
There is only one section of code to debug (only have to debug a subroutine).
There is only one section of code to update (improvements and extensions of the code are available everywhere the subroutine is called).
A function is a subroutine that can receive multiple parameters and returns a single value. A function always returns a value through its identifier.
A procedure is a subroutine that can receive and return multiple parameters. It may or may not return a value. If values are returned, they are returned via parameters.
A parameter is data or values that are passed to, or received from, a subroutine.
Function in pseudocode example:
FUNCTION Multiply (Num1 : REAL,
Num2 : REAL) RETURNS REAL
RETURN Num1 * Num2
ENDFUNCTION
Procedures are the same but with ‘PROCEDURE’ and ’ENDPROCEDURE’.
Passing by value is where the subroutine holds a local copy of the data passed. Any changes made to the data are held in the local copy within the subroutine. If the data passed originated from a variable in the main code, the original value in that variable remains unaltered.
Passing by reference is where the subroutine is passed a value by reference to a variable or array declared in the main code that holds the data to be used. Any changes made to that value is stored in the referenced variable in the main code.
File handling is programming statements that allow text files to be opened, read from, written to, and closed.
A text file is a file that stores data as text. It normally has the file extension .txt.
Writing data to a file is the process of saving data to a text file.
Writing data to a file steps:
Open the file so it can have data written to it. OPENFILE Score.txt FOR WRITE
Write the data to the file. WRITEFILE Score.txt, HighScore
Close the file once the writing process has been completed.CLOSEFILE Score.txt
StreamWriter is a class library within Visual Basic that provides access to the code that is used to create text files and save data to text files.
Reading data from a file is the process of reading the data held in the text file.
Reading data from a file steps:
Open the file. OPENFILE Score.txt FOR READ
Read data from the file. READFILE Score.txt, StartHighScore
Close the file. CLOSEFILE Score.txt
Subroutines are called from another routine. Once they have completed execution, they pass control back to the calling routines.
The main differences between procedures and functions are:
Functions return a single values through their identifier.
Procedures return zero or more values through their parameters.
Parameters can be passed by value (ByVal) or by reference (ByRef). By value parameters hold a local copy of the value passed. By reference parameters are passed the reference to an external data location that they use as a variable. It is crucial that return parameters in a procedure as defined as ByRef.
Text files provide a method of permanently storing data. They store data as text and usually have .txt extension.
Before a file can be used, it must be opened.
When opening a text file, it is crucial that the required method of operation (write or read) is indicated.
Once a read or write operation is complete, the text file must be closed.
A loop structure can be used to write multiple values to a text file. This can be helpful when permanently saving data from an array.
A scope is a section in code where the variable, or constant, can be accessed.
A global scope is where the variable or constant can be accessed from any part of the program.
A local scope is where the variable or constant can only be accessed in the subroutine it is declared within.
A library routine is a pre-written subroutine that can be called from within a program.
A maintainable program is a program that has key features to help it be understood at a later date.
If a program halts unexpectedly with the error message ‘File not found’ whilst trying to read data from a file, you need to, before trying to open the file, check the file exists, and if it doesn’t, then output a suitable error message.