Sub-programs Flashcards
Modularity
The concept of breaking a large program or problem down into smaller chunks
Subprogram
Self-contained section of code that performs a task
Difference between procedures and functions
Procedures do not return a value. Functions do return a value.
Parameter
An item that is passed into a function or procedure
Argument
The value passed into a parameter
Benefits of sub programs
- Allows several programmers to work on an individual program at the same time
- Code is easier to debug, update, modify and understand
- Can be reused to solve similar problems
Parameter passing by value
Value created for a subroutine being called is a copy of the original.
The parameter is held in a separate memory location therefore is only available to that subroutine.
Copy is now a local variable of that subroutine.
Parameter passing by reference
A pointer that contains the memory address of the original variable is created.
Any change to that value from within the called subroutine will also affect the value of the original variable.
Difference between passing by val and ref
By ref:
- The location (in memory) of the data is used. Any changes are retained after the procedure or function has been completed.
By val:
- A local copy of the data is use. This is discarded when the subprogram exits.