In this tutorial, we will understand Recursion using Flowgorithm flowchart. In the earlier example using Function, we have noticed that the Main flowchart calls the function. However, a recursive function is a function that calls itself. A function that calls itself is a recursive function.
Mathematical notation for the sum is
Alternatively, we can define the sum series as:
Let’s design a recursive function to compute the sum of N positive numbers called RSum. Sum of n numbers in recursive format:
Base case: When n= 1 the sum is 1. This is the halting case. We should tell the function to stop the recursive calls.
RSum = n + RSum(n-1)
This in turn changes to in the next recursion:
RSum = n + (n-1) + RSum(n-2)
and so on until we reach the base case.
RSum is the recursive function. The Main function prompts the user for the number N. The Main function invokes the recursive function with the parameter N.
The recursive function RSum calls itself to compute the sum of N natural numbers.
Execute the Main flowchart and verify the output.
If the flowchart is N the flowchart output of the recursive function should be:
Flowgorithm Tutorials Link: