4.1 Fundamentals of programming Flashcards
What is a recursive subroutine?
One that calls itself (or is defined in terms of itself)
3 advantages of recursion
- Can lead to very elegant, short code
- People think recursively (sometimes)
- It is sometimes the only way to solve a problem
2 disadvantages of recursion
- Can be slower than iterative solutions
- Can use more memory than iterative solutions
What is a global variable?
A variable that can be accessed from any part of a program
What error will occur if a recursive subroutine never meets its base case?
Stack overflow
3 items stored in a stack frame
- Return address
- Parameters
- Local variables
What is meant by a ‘base case’?
The terminating situation in a recursive procedure that does not use recursion to produce a result
What is meant by iteration?
Repeating an instruction
What is a subroutine? (2*)
- A named block of code containing a set of instructions designed to performed a frequently used operation
- It can be called from other parts of the program using its identifier
What type of iteration is it when the number of repetitions is not required to be known before the loop starts?
Indefinite iteration
2 advantages of using constants over hard-coded values
- Constants can be given identifier names, making code easier for a human to understand
- When updating a value, constants only need updating at one position in code
What is the advantage of favouring composition over inheritance?
It makes it easier to test each class using unit testing
Similarity between composition and aggregation
Both “has a” relationships (ownership)
Difference between composition and aggregation
- In composition, if the containing object is destroyed so are the objects it contains
- This is not the case with aggregation
(composition = strong ownership, aggregation = weak ownership)
5 advantages of using subroutines
- Code re-use
- Allows for modularisation of the program
- It makes it easier to identify bugs
- It makes it easier to test individual tasks NE easier to test
- It makes it easier for other programmers to interpret and understand your code
What is a syntax error? (2*)
- Occurs before program is run
- Code can’t be compiled
What is a runtime error? (2*)
- Occurs while the program is running
- Causes the program to crash