P4 Sec 19.2) Recursion: Syllabus Content
P4 Sec 19.2) Recursion: Syllabus Content
Syllabus Content:
19.2 Recursion
show understanding of recursion
Notes and guidance
Essential features of recursion.
How recursion is expressed in a programming Language.
Write and trace recursive algorithms
Show awareness of what a compiler has to do to translate recursive program code.
Notes and guidance
Use of stacks and unwinding
What do Russian dolls have to do with algorithms? Just as one Russian doll
has within it a smaller Russian doll, which has an even smaller Russian doll
within it, all the way down to a tiny Russian doll that is too small to contain
Recursion
A very efficient way of programming is to make the same function work over and over
again in order to complete a task.
Step by step, this is what happens. Recursion winds and then unwinds.
A classic computer programming problem that make clever use of recursion is to find
the factorial of a number. i.e. 4 factorial is
4! = 4 x 3 x 2 x 1
Lets see how Recursion winds and unwinds in program given below:
Or
FUNCTION Fictorial ( n : INTEGER) RETURS INTEGER
IF n = 0
THEN
Result 1
ELSE
Result n * Fictorial (n-1)
END IF
RETURN Result
END FUNCTION
Advantage of recursion
Disadvantage of recursion
A faulty recursive function would never end and would rapidly run out of memory
or result in a stack overflow thus causing the computer to freeze.
Can be difficult to debug as it can fail many levels deep in the recursion
Makes heavy use of the stack, which is a very limited resource compared to
normal memory.
Recursion When to Use: You would process the list starting at the head or tail and
then recursively traverse the list using the pointers. A tree is another case
where recursion is often used Recursions are used when you satisfy of these
conditions:
To understand this you just need to understand how a compiler interpret a function.
The compiler does not need to know whether the function is recursive or not. It just
make CPU jump to the address of function entry and keep on executing instructions.
And that's why we can use that function even if its definition is not finished. The
compiler just need to know a start address, or a symbol, and then it would know where
to jump. The body of the function could be generated later.
However, you might want to know the Tail Recursion, that is a special case commonly
in functional programming languages. The "tail recursion" means the recursive function
call is the last statement in function definition.
A recursive function is tail recursive when a recursive call is the last thing executed by
the function.
Why do we care?
The tail recursive functions considered better than non tail recursive functions as tail-
recursion can be optimized by the compiler.
Compilers usually execute recursive procedures by using a stack. This
stack consists of all the pertinent information, including the parameter
values, for each recursive call.
When a procedure is called, its information is pushed onto a stack, and
when the function terminates the information is popped out of the stack.
Thus for the non-tail-recursive functions, the stack depth (maximum
amount of stack space used at any time during compilation) is more.
The idea used by compilers to optimize tail-recursive functions is simple,
since the recursive call is the last statement, there is nothing left to do in
the current function, so saving the current function’s stack frame is of no
use.
References:
Computer Science AS & A Level Coursebook by Sylvia Langfield & Dave Duddell
https://www.geeksforgeeks.org/tail-recursion/
https://www.khanacademy.org/computing/computer-science/algorithms/recursive-
algorithms/a/recursion
http://teach-ict.com/as_as_computing/ocr/H447/F453/3_3_6/defining_syntax/miniweb/pg22.htm
https://stackoverflow.com/questions/40796473/how-do-compilers-understand-recursion