2.2.1 Programming Techniques
No description yet. This curated deck is ready for daily study.
Card preview (24)
Define the programming construct sequence
Code is executed line-by-line, from top-to-bottom.
Define the programming construct iteration
When instructions/code are executed repeatedly.
Define condition-controlled iteration and give examples of it
Condition-controlled is when a group of instructions is repeated until a given condition is met. e.g. while, do while and repeat until loops
Define count-controlled iteration and give examples of it
When a group of instructions is repeated a specific number of times. e.g. for and for each loops
Define the programming construct branching
Also called selection. When instructions/code are only executed if a certain condition is met.
How can selection be utilised?
Through IF, ELSEIF and ELSE statements and case statement
Describe a recursive function
A recursive function calls itself. Each call will create a new copy of the values in the function and add all of the values of the copy the call is being made from to a stack. It has a base case (condition) that stops the recursive call, and can have multiple base cases.
How can recursion be utilised / how is it formatted?
Recursion is utilised when a function contains an if statement in which each if/else is followed by returning data e.g. function factorial(number) if number == 0 or 1: return 1 else: return number * factorial(number - 1); endif end function
How do recursion and iteration differ?
• Recursion uses more memory / iteration uses less memory • Recursion can run out of memory / iteration cannot run out of memory • Recursion declares new variables / iteration reuses the same variables • Recursion can express a problem in fewer lines of code / iteration can take more lines of code • Recursion will call itself / iteration doesn’t
Define variables and constants.
A named memory address that holds a value of a given data type. A variable can change as the program is running. A constant can not change while the program is running.
Define a local variable
Local variables have limited scope which means that they can only be accessed within the block of code in which they were defined.
Give 2 advantages of local variables
• Less memory is used • Self-contained so unaffected by code outside of the subroutine • Take precedence over global variables with the same name • Increases data integrity
Give disadvantages of local variables
• Cannot be accessed directly by other subroutines • It has to be passed into a subroutine as a parameter
Define a global variable
Global variables can be accessed from anywhere in the / across the whole program.
Give 2 advantages of global variables
• Value doesn’t need passing as a parameter • Don’t need to return a value • Can be accessed from anywhere in the program • Persistent, maintaining their values throughout execution, making them suited to some constants
Give disadvantages of global variables
• Reduces data integrity as changing a global variable may have an impact on another module • Uses more memory because the memory space is declared when the program starts and remains in use throughout • Makes testing / debugging more difficult as it’s difficult to test an individual block of code
Describe modularity
Modular programming is a programming technique used to split large, complex programs into smaller, self-contained modules.
Describe and compare functions and procedures
Procedures and functions are both named blocks of code that perform a specific task. While procedures do not have to return a value, functions must always return a value.
What is the advantage of a procedure over a function?
• Procedures can return multiple values whereas a function must return one, single value
Define a parameter
A parameter is a piece of data that is input into a code block so that it can complete its task. Parameters are the variables that are called from the main body of the program to be used in a subroutine. They are written in brackets.
Define an argument
Arguments are the value of a parameter
Describe parameter passing by value
When a parameter is passed by value, it is effectively treated as a local variable; a copy of the value is passed to the subroutine and discarded at the end therefore its value outside of the subroutine will not be affected.
Describe parameter passing by reference
When a value is passed through a subroutine, the subroutine receives the memory location of where the data is stored, allowing it to be directly modified.
What are the benefits of parameter passing by value over passing by reference?
• The original value isn’t changed, allowing it to continue to be utilised. • Retaining the original data preserves data integrity, and prevents changes made to the parameter leading to side effects throughout the program • Suitable for small data structures • Enhances function reusability as the function doesn’t unexpectedly affect data • Easy to implement and understand, while passing by reference requires careful handling to prevent unintended changes