none
How the Subprograms(under itself) can still run after the "distance" became < 0 ??(Thanks!!) RRS feed

  • Question

  • angle = 30
    delta = 10
    distance = 60
    Turtle.Speed = 9
    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.PenColor = "LightGreen"
    DrawTree()

    Sub DrawTree
       If (distance > 0) Then
         Turtle.Move(distance)
         Turtle.Turn(angle)
         Stack.PushValue("distance", distance)
         distance = distance - delta
         DrawTree()
         Turtle.Turn(-angle * 2)
         DrawTree()
         Turtle.Turn(angle)
         distance = Stack.PopValue("distance")
         Turtle.Move(-distance)
       EndIf
    EndSub

    Tuesday, October 25, 2011 4:10 PM

Answers

  • Recursion, where a subroutine calls itself can be hard to follow.  Put some WriteLines in to see what is happening, when the distance gets to 0 (never negative) the bottom of the stack is reached and it starts to unwind to earlier called versions of the DrawTree sub.

    Also, set delta = 30 to make it simpler.

    Sub DrawTree
      TextWindow.WriteLine("Distance "+distance)
      If (distance > 0) Then
        Turtle.Move(distance)
        Turtle.Turn(angle)
        Stack.PushValue("distance", distance)
        distance = distance - delta
        DrawTree()
        Turtle.Turn(-angle * 2)
        DrawTree()
        Turtle.Turn(angle)
        distance = Stack.PopValue("distance")
        Turtle.Move(-distance)
      EndIf
    EndSub

    Tuesday, October 25, 2011 6:18 PM
    Moderator
  • Blue Bear,

    you should ask small basic questions here: http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Tuesday, October 25, 2011 2:09 PM
  • All variables in SmallBasic are Global so they persist in DrawTree, however may times it calls itself.

    When it gets to distance = 0, nothing inside the DrawTree is done and it returns - where - well to the DrawTree that called it.  Note that the original call to DrawTree only ends when all of the sub calls have completed.

    Every time DrawTree is called the WriteLine I added shows the current value for diatance.

    Initially it is 60, some drawing is done, then it is reduced to 40 and DrawTree called with distance 40, some drawing is done, then distance reduced to 20 ... finally when distance = 0, this DrawTree returns and the calling DrawTree completes to the point where the stack unwinds distance to be the last value before it was reduced (20).

    We see the path taken with the TextWindow output as distance reduces and then increases.

    The turn(angle) and turn(-angle*2) just redirect the turtle by angle clockwise and 2*angle anit-clockwise respectively.

     

     

    Tuesday, October 25, 2011 9:45 PM
    Moderator

All replies

  • angle = 30
    delta = 10
    distance = 60
    Turtle.Speed = 9
    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.PenColor = "LightGreen"
    DrawTree()

    Sub DrawTree
      If (distance > 0) Then
        Turtle.Move(distance)
        Turtle.Turn(angle)
        Stack.PushValue("distance", distance)
        distance = distance - delta
        DrawTree()
        Turtle.Turn(-angle * 2)
        DrawTree()
        Turtle.Turn(angle)
        distance = Stack.PopValue("distance")
        Turtle.Move(-distance)
      EndIf
    EndSub

    • Merged by Mark Liu-lxf Thursday, October 27, 2011 2:18 AM duplicate thread
    Tuesday, October 25, 2011 1:56 PM
  • Blue Bear,

    you should ask small basic questions here: http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Tuesday, October 25, 2011 2:09 PM
  • Hannes,

    Okay, I will post my question there.

    Please kindly let me know if any answer, thank you!!^_^

    Blue bear

    Tuesday, October 25, 2011 4:16 PM
  • Recursion, where a subroutine calls itself can be hard to follow.  Put some WriteLines in to see what is happening, when the distance gets to 0 (never negative) the bottom of the stack is reached and it starts to unwind to earlier called versions of the DrawTree sub.

    Also, set delta = 30 to make it simpler.

    Sub DrawTree
      TextWindow.WriteLine("Distance "+distance)
      If (distance > 0) Then
        Turtle.Move(distance)
        Turtle.Turn(angle)
        Stack.PushValue("distance", distance)
        distance = distance - delta
        DrawTree()
        Turtle.Turn(-angle * 2)
        DrawTree()
        Turtle.Turn(angle)
        distance = Stack.PopValue("distance")
        Turtle.Move(-distance)
      EndIf
    EndSub

    Tuesday, October 25, 2011 6:18 PM
    Moderator
  • Thanks a lot, I followed you instructions and still wonder about:

    1. When the "distance" continuously minus the "delta" and got the value below(or equal to) zero,
        why the "Sub DrawTree" can still run? since its condition is set as "If (distance > 0) Then..."

    2. When the turtle arrives a stop point, it has three results:
       (1) turn(angle) and continuous go.
       (2) turn(angle), turn(-angle*2) and turn(angle), then go back.
       (3) turn(-angle*2) and continuous go.
       For the last two results, how does the program(syntax) direct the turtle?

    Tuesday, October 25, 2011 8:32 PM
  • All variables in SmallBasic are Global so they persist in DrawTree, however may times it calls itself.

    When it gets to distance = 0, nothing inside the DrawTree is done and it returns - where - well to the DrawTree that called it.  Note that the original call to DrawTree only ends when all of the sub calls have completed.

    Every time DrawTree is called the WriteLine I added shows the current value for diatance.

    Initially it is 60, some drawing is done, then it is reduced to 40 and DrawTree called with distance 40, some drawing is done, then distance reduced to 20 ... finally when distance = 0, this DrawTree returns and the calling DrawTree completes to the point where the stack unwinds distance to be the last value before it was reduced (20).

    We see the path taken with the TextWindow output as distance reduces and then increases.

    The turn(angle) and turn(-angle*2) just redirect the turtle by angle clockwise and 2*angle anit-clockwise respectively.

     

     

    Tuesday, October 25, 2011 9:45 PM
    Moderator
  • Appreciate you for the explanation.

    And I would like to have my question in another way.

    What will happen if we embedded a subroutine in itself?

    For example, in the program mentioned above, DrawTree() was called for 2 times in itself, and what will computer do for that?

    Thank You!!

    Friday, October 28, 2011 10:14 PM