2.2.1 Programming Techniques Flashcards
Programming Constructs
Programming constructs : Represent a programs control flow, used in structured programming.
Sequence - Code executed line by line from top to bottom.
Selection - Code ran if specific condition is met.
Iteration - Block of code repeated/executed certain number of times or while a condition is met due to a loop.
Local Variables
Local Variables : Can only be accessed within block of code defined in
Advantages :
- Ensures subroutines are self-contained with no danger of variables affecting / being affected by other code AND can reuse in other programs without needing to take global variables too.
- Requires less memory as deleted when subroutine completes.
Disadvantages :
- Must be passed in by parameters into other subroutines as cannot be directly accessed by other subroutines (by value or by reference depending on situation)
- Difficult to trace and follow where variables are passed.
Global Variables
Global Variables : Can be accessed by whole program.
- often used for variables that don’t need to be changed (e.g. Date, Pi)
Advantages :
- Useful if needed by multiple parts of the program - inefficient to pass into many subroutines as parameters
- Easier to follow and program
Disadvantages :
- Can be unintentionally overwritten by other parts of program.
- Require more memory as not deleted until program terminates so memory inefficient.
Recursion
Recursion : Programming construct where subroutine calls itself until base case is met.
- Each recursive call makes a new copies of values that are added to a stack (if passing in by val)
-When base case met it unwinds popping values back off of stack.
Advantages :
- Can represent problems in fewer lines of code - less prone to errors.
- Some functions naturally recursive - suited to some problems
- Can reduce size of problem with each call
- More natural to read
Disadvantages :
- Inefficient use of memory - prone to stack overflow (stack full) if subroutine called too many times (Uses more memory).
- Difficult to trace.
Integrated Development Environment
IDE : Program with a set of tools to aid in development of code.
- Help debug code (Breakpoints, Stepping, Variable Watch Window)
- Help write code (auto indentation, autocomplete etc)
Features :
- Breakpoints : Stop programming running at a point to check variables - Debug sections of code.
- Syntax highlighting : To identify key words, variables and help identify syntax errors.
- Stepping : Run the program line by line to check result and watch variables at each stage to see where errors are.
- Auto-complete : When you start writing a command / identifier it completes it.
- Variable watch : View how variables change while program executes.
Text editors
Text editor : Does not have any helpful debugging tools or features to help develop program.
- Forced to learn syntax
- Smaller CPU usage
Recursion vs Iteration
Recursion:
- Some Problems more Suited to be coded Recursively - more elegant solutions.
- Recursion can represent problem in Fewer lines of code whereas iteration in more lines.
- Harder to trace Recursion than iteration.
- Uses more memory as new stack frame created each call whereas iteration cannot run out of memory as same variables used throughout.
- Recursive code can be Easier to understand than iterative.
Parameters, By Value + By Reference
Parameters : items passed to a sub-problem / Values passed into a subroutine.
By Value : Passing by value sends the a copy of the value, so the original will not be changed when variable is altered in subroutine and changes lost as copy of variable changed.
- Changes made need to be returned to be accessed by rest of program.
- When don’t want to change variable, only access or simulate changes that shouldn’t be propagated to original in main program.
- used often in recursion where don’t want to override values
By reference : Passing by reference sends the memory address / Pointer of the original value, so this variable will be changed for whole program when variable is changed in subroutine as change original variables value.
- Changes automatically accessible to rest of program
- When you want to change original variables value.