# How to use RECURSION ? • ### Question

• Hello?

I want to make
fractal program, use recursion.

How to make recursion in SmallBasic ?

-----------------------------------------------------------------------------------

Turtle.Speed = 10
Turtle.X = 10
Turtle.Y = 400
Turtle.Turn(90)

l = 2

koch5()

Sub koch1
Turtle.Move(l)
Turtle.Turn(-60)
Turtle.Move(l)
Turtle.Turn(120)
Turtle.Move(l)
Turtle.Turn(-60)
Turtle.Move(l)
EndSub

Sub koch2
koch1()
Turtle.Turn(-60)
koch1()
Turtle.Turn(120)
koch1()
Turtle.Turn(-60)
koch1()
Endsub

Sub koch3
koch2()
Turtle.Turn(-60)
koch2()
Turtle.Turn(120)
koch2()
Turtle.Turn(-60)
koch2()
Endsub

Sub koch4
koch3()
Turtle.Turn(-60)
koch3()
Turtle.Turn(120)
koch3()
Turtle.Turn(-60)
koch3()
Endsub

Sub koch5
koch4()
Turtle.Turn(-60)
koch4()
Turtle.Turn(120)
koch4()
Turtle.Turn(-60)
koch4()
Endsub

----------------------------------------------------------------
Tuesday, November 9, 2010 1:17 AM

• Nice program.

Recursion is where one way or another a subroutine ends up calling itself - clearly repeating patterns such as fractals is a good example of using recursion.

Here is your example slightly modified with 7 levels of recursion (needs a slightly wider window).

Turtle.Speed = 10
Turtle.X = 10
Turtle.Y = 400
Turtle.Turn(90)

l = 2
level = 7
angle = 70
koch()

Sub koch
level = level-1
If (level = 0) Then
Turtle.Move(l)
Else
koch()
Turtle.Turn(-angle)
koch()
Turtle.Turn(2*angle)
koch()
Turtle.Turn(-angle)
koch()
EndIf
level = level+1
Endsub

• Marked as answer by Wednesday, November 10, 2010 7:12 AM
Tuesday, November 9, 2010 7:04 PM

### All replies

• Nice program.

Recursion is where one way or another a subroutine ends up calling itself - clearly repeating patterns such as fractals is a good example of using recursion.

Here is your example slightly modified with 7 levels of recursion (needs a slightly wider window).

Turtle.Speed = 10
Turtle.X = 10
Turtle.Y = 400
Turtle.Turn(90)

l = 2
level = 7
angle = 70
koch()

Sub koch
level = level-1
If (level = 0) Then
Turtle.Move(l)
Else
koch()
Turtle.Turn(-angle)
koch()
Turtle.Turn(2*angle)
koch()
Turtle.Turn(-angle)
koch()
EndIf
level = level+1
Endsub

• Marked as answer by Wednesday, November 10, 2010 7:12 AM
Tuesday, November 9, 2010 7:04 PM
• Thanks a lot.

Honestly, I did not fully understand. ^^;

-------------------------------------------

.

.

.

level = level + 1

.

-------------------------------------------

Wednesday, November 10, 2010 3:13 AM
• solongos,

Basically, every time we enter koch we go down a level and every time we leave it we go back up a level.  When the level is 0 we do the drawing.  The levels above 0 we just rotate the turtle which creates the fractal repeating shapes at different levels.

To better see what is happening, add a TextWindow.WriteLine(level) at the start of the routine just after level = level-1 to see how it progresses.

All the recursion is doing is exactly the same as your original code, just using the level to keep track of where it is but using the same subroutine - just as your koch5 (level 5) called koch4 (level set to 4) which called koch3 (level set to 3) etc and as it returned from koch3 (level 3) it went to koch4 (level 4) etc.

Also you can add colours to the lines to see how it works.

Finally, when there are lots of lines the turtle gets slow since the Turtle draws shape objects and not directly on the background, so you can replace the turtle with line drawing to speed it up (factors of 100+ faster for larger fractals).

Wednesday, November 10, 2010 6:39 PM
• Small Basic is Fun ^.^

Help me, One more...

I make Sierpiński Carpet. How to change to Recursion.

---------------------------------------------------------------

x = 20
y = 20
l = 20

sp_3()

Sub box
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillRectangle(x,y,l,l)
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
GraphicsWindow.FillRectangle(x+l/3,y+l/3,l/3,l/3)
EndSub

Sub sp_1
box()
x = x+l
box()
x = x+l
box()
y=y+l
box()
y=y+l
box()
x=x-l
box()
x=x-l
box()
y=y-l
box()

y=y-l
endsub

Sub sp_2
sp_1()
x = x+3*l
sp_1()
x = x+3*l
sp_1()
y=y+3*l
sp_1()
y=y+3*l
sp_1()
x=x-3*l
sp_1()
x=x-3*l
sp_1()
y=y-3*l
sp_1()

y=y-3*l

EndSub

Sub sp_3

sp_2()
x = x+9*l
sp_2()
x = x+9*l
sp_2()
y=y+9*l
sp_2()
y=y+9*l
sp_2()
x=x-9*l
sp_2()
x=x-9*l
sp_2()
y=y-9*l
sp_2()

y=y-9*l

EndSub

---------------------------------------------------------

Kim Youngju, Korea CS Teacher. Love Coding & Programming !
Thursday, November 11, 2010 6:11 AM
• A little more complicated because the calculation of x and y changes in each level, but still much the same idea as before.  I introduced a variable z which is the mutiplier to be used for each level.

x = 20
y = 20
l = 20
level = 4
z = 1
For i = 2 To level-1
z[i] = 3*z[i-1]
EndFor

box()

Sub box
level = level-1
If (level = 0) Then
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillRectangle(x,y,l,l)
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
GraphicsWindow.FillRectangle(x+l/3,y+l/3,l/3,l/3)
Else
box()
x = x+z[level]*l
box()
x = x+z[level]*l
box()
y=y+z[level]*l
box()
y=y+z[level]*l
box()
x=x-z[level]*l
box()
x=x-z[level]*l
box()
y=y-z[level]*l
box()
y=y-z[level]*l
EndIf
level = level+1
EndSub

Thursday, November 11, 2010 6:49 PM