2.2.1 - Programming Techniques Flashcards
Define the programming construct Sequence
All instructions are executed once in the order in which they appear.
Define the programming construct Iteration
A section of code loops or a group of statements/instructions are executed repeatedly for a set number of times or until a condition is met.
Define the programming construct Selection
A condition is used to determine which of the sections of code (if any) will be executed. As a result some instructions may not be executed.
What is Recursion?
When a function/procedure calls itself from within the function.
What are some features of a recursive function
- Contains a stopping condition called the base case; there may be more than one.
- Stopping condition should be reachable in a finite number of times
- For any other input value other than the stopping condition, the function should call itself
List some advantages of using recursive function rather than an iterative function
- Quicker to write/generally less lines of code as some functions are naturally recursive.
- Suited to certain problems for example those using trees.
List some disadvantages of using recursive function
- Creates new variables for each call therefore is less efficient use of memory.
- Is more likely to run out of stack space/memory due to too many calls causing it to crash.
- Requires more memory and resources than the equivalent iterative algorithm.
- Algorithm may be more difficult to trace/debug as each frame on the stack has its own set of variables.
Define a Global Variable
A variable that is usually declared and defined at the start of a program outside subprograms. Is visible and available everywhere throughout the whole program in all modules including functions and procedures.
Define a Local Variable
- A variable declared and defined within one subroutine and is only accessible and visible within that subsection of the program or construct where it is created.
- Can be used as parameters
When is a Local Variable Destroyed?
When the subprogram is exited
When is a Global Variable Destroyed?
When the whole program ends
Explain what is meant by ‘Scope’ in relation to Global and Local Variables.
- A range of statements/functions that a variable is valid for
- A local variable takes precedence over a global variable of the same name/allow the same identifier to be used for different purposes without conflict
Disadvantages of Global Variables
- It increases the complexity of a program.
- It may be changed inadvertently when the program is complex.
- They can easily be accidentally altered because Local variables with the same name as global variables will override and take precedence over the values in the global variable.
What is an advantage of a local variable
The same variable names within two different modules will not interfere with one another to allow the same identifier to be used for different purposes without overwriting values or causing errors
What is Modularity
- Program is divided into separate, self-contained, specific modules or tasks.
- Modules can be subdivided further into smaller modules.