S11 - 56 subroutines and recursion Flashcards
what is recursion?
Recursion is when a subroutine (often a function) calls itself from within its own subroutine
what three characteristics should a recursive subroutine have?
+ A stopping condition
+ the stopping condition should be reachable in a finite number of times
+ for any input value other than the stopping condition the subroutine should call itself (recursion)
what happens if a recursive subroutine calls itself too many times?
A recursive subroutine may call itself indefinitely and cause a stack overflow
what is a subroutine?
A subroutine is a named block of code which performs a specific task within a program.
what are the two common types of subroutines?
functions and procedures
how is a subroutine called?
A subroutine is called by writing its name in a program statement.
some functions return a result and…
some do not
A procedure is called by…
writing its name but not assigning the result to a variable
describe what happens if a parameter is passed by value
If a parameter is passed by value its actual value is passed to the subroutine
describe what happens is a parameter is passed by reference
if a parameter is passed by reference the address (not the value) is passed to the subroutine
whats the difference between a parameter being passed a reference and a value
unlike value when a parameter is passed by reference the address is passed to the subroutine not the value.
what are global variables?
variables used in the main program by default are global variables
state an advantage of a global variable
Global variables can be used anywhere in the program, including within subroutines
how long do local variables last?
Local variables only exist during the execution of a subroutine.
state one disadvantage of local variables
Local variables cannot be accessed outside the subroutine