Section 11: Programming techniques Flashcards
What is passing a parameter by value?
If a parameter is passed by value, its actual value is passed through the subroutine where it is treated as a local variable. Therefore, if you change the parameter inside the subroutine its value in the main program will not change.
What is passing a parameter by reference?
If a parameter is passed by reference, its address is passed through the subroutine so that when the variable is written to inside the subroutine, it is also written to inside the main program.
What are the advantages of modular programming?
Advantages of modular programming:
- Easier to find errors
- Subroutines can be tested independently, which shortens the time to get a big program working
- Once a subroutine has been tested you can re-use it throughout the program
- Easier for a team of programmers to work on
- Makes code neater and easier for other programmers to read
What are the 3 essential characteristics of recursion?
3 essential characteristics of recursion:
- Must include a stopping condition or base case
- Routine must call itself for input values other than the stopping condition
- Stopping condition must be reached after a finite number of calls
Why is encapsulation important?
Encapsulation is important because it allows the developer to test a subroutine independently and use it in many different programs without having to know what variables it uses. Also, it stops there being conflict where two variables may coincidentally have the same name in a program.
How is the call stack used when calling a subroutine?
Each time a subroutine is called, the address, parameters and local variables used are held in a stack frame in the call stack, which then subsequently unwinds once the end of the subroutine is reached.