2. 2. 1 Programming Techniques Flashcards
1
Q
Programming constructs
A
- Constructs used to represent programs control flow in structured programming
- The 3 constructs are Sequence, Selection/Branching and Iteration
- Sequence, code executed line-by-line, top to bottom
- Selection/Branching, certain block of code ran if specific condition met (IF statements)
- Iteration, block of code executed certain number of times or while condition is met (FOR, WHILE, REPEAT UNTIL loops)
- Either count-controlled (FOR loop) or condition-controlled (WHILE loop)
2
Q
Recursion
A
- Programming construct, subroutine calls itself during its execution
- Continues till certain condition (stopping condition) is met, at this point it would stop
- Recursion produces same results as iteration, more suited to certain problems
- Advantage, can be represented in fewer lines, less prone to human error
- Essential that recursive subroutines are clearly defined, so that it stops after a finite number of function calls
- Example of a recursive function, factorial
- Each time function calls itself, new stack frame created within the call stack where parameters, local variables and return addresses are stored
- This info allows subroutine to return to particular point during execution
- A call stack is a LIFO data structure that stores details about the order that subroutines have been called, a stack frame is a frame of data that gets pushed onto the stack
- Finite of stack frames created until stopping condition (base case) reached, at this point subroutine unwinds
- This refers to process of info from call stack being popped off the stack
3
Q
Disadvantages of recursion
A
- Inefficient use of memory, if subroutine calls itself too many times there is a danger of stack overflow (call stack runs out of memory) causing program to crash
- Tail recursion, form of recursion implemented in more efficient way, less stack space required
- Another issue with recursion, difficult to trace, especially with more and more function calls
4
Q
Global and Local Variables
A
- Variables can either be defined with either global or local scope, scope refers to section of code in which variable is available
- In the event that a local variable exists within subroutine with same name as a global variable, local variables will take priority
5
Q
Local Variables
A
- Local variables, limited scope, only accessed within block of code that it was defined in
- For example, variable defined in subroutine, can only be accessed within that subroutine
- Multiple local variables with same name can exist in different subroutines as a result, they do not affect each other
- Using local variables is considered good programming practice, ensures subroutines are self-contained with no danger of variables being affected by code outside the subroutine
6
Q
Global Variables
A
- Accessed across whole program, all variables used in main body automatically declared to be global
- Useful for values that need to be used by multiple parts of the program
- Not recommended, can unintentionally overwrite and edit these variables
- GV not deleted till program terminates, therefore require more memory than LV which are deleted once subroutine has been completed
7
Q
Modularity, Functions and Procedures
A
- Modular programming, technique used to split large, complex programs into smaller, self-contained modules
- Essential to making problem easier to understand and approach, modular design makes it easier to divide tasks between a team
- Simplifies process of testing and maintenance, each component can be dealt with individually
- Improves reusability of components, once a module has been tested, it can be reused with confidence
- Popular technique to modularise programs, top-down approach
- Problem continually broken down into sub-problems till each can be represented as a individual, self-contained blackbox which performs a certain task, also known as stepwise refinement
- Preferred method to approach large problems, broken down into levels, higher levels provide overview of problem, lower levels specify in detail the components of this problem
- Aim of the top-down approach is single tasks that are ideally self-contained modules or subroutines (in my example, chefs not affected by tables being sorted out)
- Each of these tasks can be solved and developed, once programmed the subroutines can be tested separately before being bought together and finally integrated
8
Q
Procedures and Functions
A
- Both are named blocks of code that perform a specific task
- Procedures do not have to return a value, function must always return a value
- Procedure can return multiple values, function can only return one single value
- Procedures typically given data as parameters for manipulation
- Functions commonly make use off local variables
9
Q
Use of an IDE
A
- Integrated Development Environment (IDE), program that provides set off tools to make it easier for programmers to write, develop and debug code
- Examples of IDE, PyCharm, Eclipse, IDLE and Microsoft Visual Studio
- Common features of IDEs, stepping, variable watch, breakpoint, source code editor and debugging tools
- Stepping, allows you to monitor effect of each individual line of code by executing single line at a time
- Variable watch, useful to observe contents of a variable change in real-time during execution of program (can be used to pinpoint errors)
- Breakpoint, IDEs allow users to set point in program that it will stop, can be based on condition or be set for specific line, can help pinpoint where an error is occurring
- Source code editor, provides features such as autocompletion or words, indentation, syntax highlighting and automatic bracket completion to make the coding process easier
- Debugging tools, some IDEs provide run-time detection of errors with guides of where in the code they are likely to have occurred through line numbers and highlighting