2.2.1 Programming Techniques
No description yet. This curated deck is ready for daily study.
Card preview (24)
Programming constructs
• a programming construct determine the order in which lines of code are executed • 3 main programming constructs • selection • iteration • sequencing
Sequence
• code is executed line by line in sequence from top to bottom
Iteration
• repeating a line or block of code using a loop • can either be count controlled or condition controlled
Count controlled
• (for loop) • is used when the number of iterations is known before execution and the block of code is executed a certain number of times
Condition controlled
• used when the number of iterations is not known so the block of code is only executed while a condition is met • while loop, do until loop
Branching
• the particular block of code is run if a specific condition is met • eg. If else statements
Recursion
• is when a subroutine/function calls itself from within its own subroutine/function [1] • the function continues to call itself until a base case/stopping condition is met [1]
Importance of base case
• without a base case, a recursive function may call itself indefinitely resulting in a stack overflow • the base case should be reachable within a finite number of times • for input values other than the stopping condition, the function must call itself
Recursion benefits
• quicker to write/ requires less lines of code [1] as some functions are naturally recursive • can reduce the size of a problem with each call [1] • recursion is suited to certain problems e.g problems involving trees or factorials [1]
Recursion drawbacks
• more difficult to trace as each frame on the stack has its own set of variables [1] • requires more memory compared to the equivalent iterative algorithm in order to maintain the stack of calls [1] • risk of stack overflow if memory runs out e.g due to too many calls [1]
Scope
• the section of code in which a variable can be accessed • a local variable within a subroutine takes precedence over a global variable with the same name
Local variables
• can only be accessed with the subroutine in which they were defined [1] • multiple local variables with the same name can exist in different subroutines [1]/ same variable name be used in other modules without causing errors/overwriting • are deleted once the subroutine ends [1] • can overwrite global variables with the same name [1]
Global variables
• can be accessed across the whole program [1] (so no need to pass as a parameter or redeclare • is used when a value needs to be accessible from various parts of a program [1] • value remains the same irrespective of where it is accessed [1] • e.g values of constants like pi or VAT rate [1] • not deleted until the program terminates so requires more memory
Global variables drawbacks
• make it difficult to integrate modules [1] • increase the complexity of a program [1] • they may cause conflicts with names in other modules [1] • may be modified unintentionally when the program is complex [1]
Modular programming
• programs are split into separate self contained modules [1] • which can be written/tested individually [1] • modules can be subdivided further into smaller modules [1]
Modular programming advantages
• modules can be reused in the current project and future projects which saves time [1] • modules can be shared between programmers so work is easier to divide between a team [1] • this reduces development time as work takes place in parallel [1] • modules can be allocated according to the programmers/team members expertise [1] • modules are easier to maintain/test/debug as each subroutine can be tested independently before integration [1]
Function
• a named block of code that performs a specific task and must always return a single value [1]
Procedure
• a named block of code that performs a specific task but does not have to return a value [1]
Parameter
• an item of data that is passed into a subroutine and can be discarded at the end of the subroutine [1]
Parameter passing
• by value • by reference
Passing by Value
• a local copy of the value/data is passed into the subroutine and discarded at the end of the subroutine [1] • so only the copy is changed [1] • this means value of the original data is unchanged/remains unaffected [1]
Passing by Reference
• the address/memory location of the data is sent [1] • so changes are made to the original data/changes the value in the variable passed [1] • changes remain even after the subroutine exits [1]
IDE ( integrated development environment)
a program used for developing programs which provides a set of tools to make it easier for programmers to write, develop and debug code [1]
IDE features
• Stepping • break points • error diagnostics • variable watch window • syntax highlighting • autocomplete