Home

What Is Recursion - The Most Basic Example

Blog Date 20 April 2021

Loops (Do, While, For, Foreach) are as common as muck but that does NOT mean there is anything wrong with them. Recursion on the other hand was something I'd not really come across until watching the wonderful Professor Brailsford on Computerphile talking about their history (https://www.youtube.com/watch?v=HXNhEYqFo0o).

Having watched this I still didn't know what recursion is. Fear not it is not complicated. It is, rather than using a typical loop, calling a function from within the same function. Eh, what? Here, let me explain.

To write numbers incrementally with a loop we would use the following...

    protected void Page_Load(object sender, EventArgs e)
    {
        MyLoop(1, 20);
    }

    protected void MyLoop(Int32 MyStart, Int32 MyEnd)
    {
        for (int i = MyStart; i <= MyEnd; i++)
        {
            MyShow.Text += Convert.ToString(i) + "<br />";
        }
    }

So the Page_Load executes when the, errr, page loads. It calls MyLoop passing the values 1 and 20. MyLoop uses a for loop to print (using MyShow.Text) each number and adds a "<br />" to create a new line (I'm using C# ASP.Net on a web server). This is simple, effective, quick and easy to read. There is nothing wrong with this, this is the BEST way to carry out this simple task.

BUT - There's more than one way to skin a cat. In this case recursion is wordy and long winded however there may be times (I can't think of one right now) where recursion better suits your needs. This example is here merely to demonstrate in the most basic terms what recursion is and how it is used.

    protected void Page_Load(object sender, EventArgs e)
    {
        MyRecur(1, 20);
    }

    protected void MyRecur(Int32 MyStart, Int32 MyEnd)
    {
        if(MyStart <= MyEnd)
        {
            MyShow.Text += Convert.ToString(MyStart) + "<br />";
            MyStart++;
            MyRecur(MyStart, MyEnd);
        }
    }

Everything starts the same but in MyRecur we use an "if" to see if we've reached the end of our task. Then we call the SAME MyRecur function from within the MyRecur function. That is all, that is what recursion means.

Quite unlike Professor Brailsford I am no experienced computer genius. However reading this code and following the logic it raises questions within me. Please DO NOT think I know what I am talking about, I am merely pondering out loud here and I am ready to be corrected.

When I think of a function I need to put it into a tangible world. In my mind the Page_Load is a General over an army. The General shouts "Private MyRecur!! Run your function using 1 and 20!" Private MyRecur shouts "Yes Sir!" and scurries off.

Private MyRecur checks to see if 1 is less than or equal to 20. It is. MyRecur puts the number 1 into the display box as instructed then adds 1 to 1 to get 2. The next instruction is confusing though. MyRecur is supposed to instruct, well, err, themselves with the next set of instructions?

Either... Private MyRecur needs to magically create a new copy of themselves to pass the instructions to OR they need to repeat. I HOPE they choose to repeat, I worry they choose to recreate themselves - otherwise the memory space will be filled with lots of Private MyRecurs. With 1-20 this is fine but imagine if it was 1-1,000,000?


A simple web based Database for your small business? Affordable and bespoke - contact ren@techsolus.co.uk

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog