- Write an empty function.
- Write an “if-else” statement in that function.
- The condition of the “if” statement is the condition that requires you to exit the recursive function.
- Within that ‘if’ condition, write what you would want to do.
I’ve noticed it is usually a simple return value statement that is put back into the stack of ‘recursive waits’ waiting to happen. So basically that is what sets off the compute chain reaction.
- In the else statement, call the function again and put in the input of the function. The difference is that the input parameters may have some updated condition.
Here’s also where you record or return the value of the ‘if’ statement for use in some operation.
- Finally, return any values after processing the recursive part of the code i.e the ‘else’ section.
RECURSIVE SUM EXAMPLE
PS: This could probably be more elegantly written, but I prefer it this way since it keeps the format of other recursive functions I saw in my search.