Dave Drohan (SETU)

BACK NEXT

Recursion

In this step, you will work on reproducing the code example 6.11 from your lectures.

Solution with no recursion

Create a new Processing sketch in your workspace and call it Example_6_11.

Include the following code in it:

void setup()
{
  size(100,100);
  drawLines(10,4);
}

void drawLines(int x, int num)
{
    for (int i = 0; i < num; num--)
    {
        line (x, 20, x, 80);
        x += 5;
    }
}

Run the code so you can see the output.

Recap of recursion

A method can contain a line of code that calls itself.

This is called recursion.

To stop the infinite calling of the method, it is necessary to have some way for the method to exit. This is called the base case. You continually work towards the base case.

Using recursion

Rewrite the drawLines method so that it uses recursion to produce the same output as above.

Save your work.