7.5 Procedures and Functions Flashcards
Subroutines
Allow code that you intend to use a number of times to be grouped together under one name
Give some examples of built-in subroutines
- input
- int
Procedures
Execute code in a subroutine but do not return anything (can have parameters)
Functions
Execute code in a subroutine but return a value
What are parameters?
Parameters are inputs to subroutines which can be used to affect the output or the process, for example, a parameter could be the age of a man or the name of a person
Advantages to subroutines
- They are useful to break up a large program into self-contained units
- Improves code readability
- Improves code maintenance
- Improves workflow (reduces development time)
- Can be used in other programs
What is the scope of a variable?
The scope determines where the variable can be used and recognised in the program
Local scope
Variable can only be referenced and used where the instance was made (for example if it was initialised inside a function it can only be inside the function)
Global scope
A variable that can be used anywhere in the code despite it being initiated in a closed and a local area (for example a function)
Advantages of using local variables
Local variables keep a subroutine self-contained, so it can be used in any program without variable names conflicting with those used in the calling program