User Tools

Site Tools


notes:discrete:fall2023:projects:mor0

This is an old revision of the document!


Table of Contents

MOR0

Matrix

Recursion

The concept of recursion is actually simple but implementing it is more challenging than you might think. Recursion works by creating a function that calls itself until it hits a base case to end/breakout of the recursion function. Along with the base case, it has another case that slowly leads to the base case.

A simple implementation of this can be seen with a function that gets the factorial value given a certain number.

Here is what that function looks like:

// Factorial function using recursion
int factorial(int n)
{
    // Base case for when the number given is 0
    if (n == 0)
    {
        return 1;
    }
 
    // Takes the given value and multiplies it by the previous factorial value
    else
    {
        // Function calls itself (calls n-1) until it reaches the base case (0)
        return n * factorial(n - 1);
    }
}
notes/discrete/fall2023/projects/mor0.1699490612.txt.gz · Last modified: 2023/11/09 00:43 by wgates1