Programming Techniques Flashcards
What are the four main structures used in structured programming
- Sequence - code is executed line-by-line
- Selection - a certain block of code is run if a specific condition is met
- Iteration - a block of code is executed a certain number of times or while a condition is met
- Recursion - the function calls itself from within the function
What are the Advantages and Disadvantages of Recursion
Advantges -
- Programmers can be written in fewer lines of code
- Makes code less prone to errors
Disadvantges -
- Its inefficient use of memory
- Stack overflow cam occur which is when the call stack runs out of memory. This would cause the program to crash
- Can be difficult to trace if using many function calls
What are the Differences between Iteration and Recursion
- Recursion uses more memory, Iteration uses less memory
- Recursion uses a new set of variables (previously placed on stack), iteration uses same variables
- Recursion can run out of memory, Iteration will not run out of memory
- Recursion can express a problem elegantly (less lines of code), Iteration can take up more lines of code and is harder to follow
What is meant by the term Scope
The section of code in which a variable is available
What scope does a Local Variable have
- Limited scope
- They can only be accessed within the block of code in which they were defined.
- If a local variable is defined within a subroutine, it can only be accessed within that subroutine.
- Therefore, multiple local variables with the same name can exist in different subroutines and will remain unaffected by each other
Why are using Local Variables a good way to program
It ensures subroutines are self-contained, with no danger of variables being affected by code outside of the subroutine
What is the Scope of a Global Variable
- Can be accessed across the whole program
- All variables used in the main body of a program are automatically declared to be global
- These are useful for values that need to be used by multiple parts of the program
What are the Disadvantages of using Global Variables
- Global Variables can be unintentionally overwritten and edited (Local Variables take priority)
- Global variables are not deleted until the program terminates so they require more memory than local variables which are deleted once the subroutine has been completed
What is Modular Programming
A programming technique used to split large, complex programs into smaller, self-contained modules
What is the benefit of using Modular Programming
- Makes it easier to divide tasks between a team and manage
- Simplifys the process of testing and maintenance, as each component can be dealt with individually
- Improves the reusability of components
- The Top-Down method is an example of Modular Programming
What is Stepwise Refinement
Different name from the Top-Down method
What are the different between a procedure and function
- Procedures do not have to return a value whilst functions always return a value
- Procedures can return multiple values whereas a function must return one single value
- Procedures are typically given data as parameters while functions commonly make use of local variables
What is Passing by Value and Passing by Reference
Passing by Value -
- Sends the actual value being used
- This data is held in a separate memory location and is only available to the subroutine
- Even if the value of the parameter is changed within the subroutine, the value of the original argument is unchanged
Passing by Reference -
- This creates a pointer that contains the memory address of the original variable
- Therefore, any change to the parameter within the subroutine also affects the value of the original variable
What is an IDE (Integrated Development Enviroment)
A program which provides a set of tools to make it easier for programmers to write, develop and debug code
What are some examples of IDE tools
- Stepping
- Variable watch
- Breakpoint
- Source code editor
- Debugging tools