1/3
Made from Ada Comp. Sci. and PMT notes
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Define recursion
A programming construct in which a subroutine calls itself during its execution
Advantages of recursion
Can represent certain problems with fewer lines of code, meaning that the function is:
Easier to read
Less prone to errors
Natural way to process certain data structures (such as trees) that are recursive by nature
Disadvantage of recursion
Harder to trace because you must keep track of multiple instances of the function.
Uses more memory because of the need to store multiple stack frames. If the call stack runs out of memory, there will be a stack overflow, and the program will crash.
Can be slower because of the need to manage stack operations.
Are parameter(s) passed by value or by reference in recursive subroutines, and why?
If the parameter is sent by value, it will be a copy of the parameter that is used so the parameter wont be overridden. This will produce the correct output.
If the parameter is passed by reference it would not produce the correct result as it would be overridden / because it is a pointer to the address of the variable.