SLR 23 - PROGRAMMING TECHNIQUES Flashcards
What are the 3 programming constructs?
- sequence
- selection
- iteration
Give an example of each programming construct
- sequence:
a = 5
b = 6
c = a x b
print(c x 2) - selection:
a = 5
b = 6
if a > 0:
print(a x b)
else:
print(“a = 0”) - iteration:
a = 5
while a > 0:
a = a -1
What is recursion?
a programming subroutine where a section of code calls itself until a base case is met
What are the 3 conditions for a recursive statement?
- contains a stopping condition (base case)
- for any input value other than the stopping condition, the subroutine should call itself (recursion)
- the stopping condition should be reachable within a finite number of times
Which is more efficient in terms of memory usage (recursion/iteration) and why?
- iteration
- recursion is not very memory-efficient. Every time a recursive function calls itself, the processor needs to remember where it was before it jumps to the new copy so it knows where to return later
- the processor also needs to remember the values of all the previous variables, as they are local to those copies of the recursive function – this is done using stacks, which takes up space in memory
What is a global variable?
a variable that is:
- “typically” declared at at the top of a program outside of any subroutine
- accessible throughout the program
- created when the program starts
- destroyed when the program ends
What is a local variable?
a variable that is:
- declared inside a subroutine
- only accessible by that subroutine
- created when the subroutine is called
- destroyed when the subroutine ends
Why is use of global variables generally considered poor programming practice?
excessive, unnecessary use of global variables can make programs hard to test, debug and maintain
What is modularity?
- the concept of breaking down a large program/problem down into smaller chunks
- the goal is to design a program in such a way that each module carries out a single, specific task
- modules may be referred to by different names:
- subroutines
- procedures
- functions
- methods
What is a procedure?
a subroutine that takes in zero, one or more parameters and performs a set task
What is a function?
a subroutine that takes in zero, one or more parameters and performs a set task and returns a value
What are the 2 methods of passing data into a subroutine?
- passing by value (byVal)
- passing by reference (byRef)
What does passing a parameter by reference mean?
passing the address or pointer of the required value into a procedure
What does passing a parameter by value mean?
creating a temporary local copy of the actual value of a variable and passing it into the procedure