From Higher Computing Science
- Parameters are items of data that can be passed between the subprograms
- To pass data into a subprogram, a list of parameters is given
- To make it easy for different people to write subprograms that work together, the name of a parameter inside the subprogram can be different from its name outside of the subprogram.
- The name given to the parameter within a subprogram is a formal parameter
- The name given to the parameter when it is called is the actual parameter.
- Actual parameters may also be numbers rather than variables, such as in the function call ROUND(30.423,1)
- If a copy of the variable passed into a subprogram is used, it will not change the original variable. This is called pass by value.
- If a reference (the address in memory) of a variable is passed into a subprogram, the subprogram can change the original variable. This is called pass by reference.
- Variables declared within a subprogram will be local variables. This means that they only exist during the lifetime of the subprogram’s execution.
- Variables declared outside of subprograms will be global variables. This means they are accessible from any part of the program.
- Global variables can be dangerous for programmers – a variable with global scope can be seen by the whole program. A global variable with a common name such as total or name may “overlap” with a local variable elsewhere. This could cause confusion.
- A subprogram is a block of code within a program with a label. Sub programs can be used more than once.
Functions, procedures and methods
A function is a subprogram that returns a value. This means when you call the function, it sends back data directly to the program, e.g. the RND, ROUND or MOD functions.
- A procedure is a block of code that runs in sequential order.
- Procedures allow programs to group together code, but more importantly, reuse it.
- A procedure can be called multiple times in a program - this saves on code duplication.
- A procedure can take parameters, which allows it to work with different items of data each time it runs.
- The difference between a function and a procedure is that a procedure is not designed to return a value to a program. Calling a procedure is simply calling a modular block of code.
- Object-oriented programs are designed around objects rather than sequences of instructions
- Each type of item used in a OO program is described using code called a class
- A class has a list of attributes and a list of methods
- Each class can be used as the blueprint to create an object, for example the code in a class called Square could describe the attributes and methods of a square, and each square the program uses would be created as an object
- Objects are closed off from other areas of the program – the design of the object determines which attributes and methods can be called from other parts of the program. This is called encapsulation.
- Each class can be used as the base code for another class, and the programmer can add on new attributes and methods. For example, the Shape class could have two subclasses called Square and Triangle.
- Creating a subclass from an existing class is called inheritance