none
How Recursive Functions works ? RRS feed

  • Question

  • Hi all,

    Can anyone explain me with neat examples , how the recursive function works?

    is function work like stack ? how ?

    regards,

    Aniruddha

    Saturday, June 9, 2012 9:03 AM

Answers

  • Indeed, when a function is called, its return memory address is stored within the stack.

    But the closer that Small Basic has of a function is a subroutine!!!

    This is not not a C# forum! @_@


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Saturday, June 9, 2012 9:56 AM
    Answerer
  • This is a sample of recursive call in Small Basic.  This code saves variables as array.  You can use Stack also to save variables.

    ' Ice
    ' Copyright (c) 2010 Nonki Takahashi
    
    GraphicsWindow.BackgroundColor = "Gray"
    GraphicsWindow.PenColor = "White"
    level = 1
    maxlevel = 5
    xc[level] = GraphicsWindow.Width / 2
    yc[level] = GraphicsWindow.Height / 2
    arm[level] = 100
    DrawIce()
    
    Sub DrawIce
      x1[level] = xc[level] - arm[level]
      x2[level] = xc[level] + arm[level]
      y1[level] = yc[level]
      y2[level] = yc[level]
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
      x1[level] = xc[level] + arm[level] * Math.Cos(Math.Pi / 3)
      x2[level] = xc[level] - arm[level] * Math.Cos(Math.Pi / 3)
      y1[level] = yc[level] + arm[level] * Math.Sin(Math.Pi / 3)
      y2[level] = yc[level] - arm[level] * Math.Sin(Math.Pi / 3)
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
      x1[level] = xc[level] - arm[level] * Math.Cos(Math.Pi / 3)
      x2[level] = xc[level] + arm[level] * Math.Cos(Math.Pi / 3)
      y1[level] = yc[level] + arm[level] * Math.Sin(Math.Pi / 3)
      y2[level] = yc[level] - arm[level] * Math.Sin(Math.Pi / 3)
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
    EndSub
    


    Nonki Takahashi

    Saturday, June 9, 2012 12:19 PM
    Moderator

All replies

  • Indeed, when a function is called, its return memory address is stored within the stack.

    But the closer that Small Basic has of a function is a subroutine!!!

    This is not not a C# forum! @_@


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Saturday, June 9, 2012 9:56 AM
    Answerer
  • This is a sample of recursive call in Small Basic.  This code saves variables as array.  You can use Stack also to save variables.

    ' Ice
    ' Copyright (c) 2010 Nonki Takahashi
    
    GraphicsWindow.BackgroundColor = "Gray"
    GraphicsWindow.PenColor = "White"
    level = 1
    maxlevel = 5
    xc[level] = GraphicsWindow.Width / 2
    yc[level] = GraphicsWindow.Height / 2
    arm[level] = 100
    DrawIce()
    
    Sub DrawIce
      x1[level] = xc[level] - arm[level]
      x2[level] = xc[level] + arm[level]
      y1[level] = yc[level]
      y2[level] = yc[level]
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
      x1[level] = xc[level] + arm[level] * Math.Cos(Math.Pi / 3)
      x2[level] = xc[level] - arm[level] * Math.Cos(Math.Pi / 3)
      y1[level] = yc[level] + arm[level] * Math.Sin(Math.Pi / 3)
      y2[level] = yc[level] - arm[level] * Math.Sin(Math.Pi / 3)
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
      x1[level] = xc[level] - arm[level] * Math.Cos(Math.Pi / 3)
      x2[level] = xc[level] + arm[level] * Math.Cos(Math.Pi / 3)
      y1[level] = yc[level] + arm[level] * Math.Sin(Math.Pi / 3)
      y2[level] = yc[level] - arm[level] * Math.Sin(Math.Pi / 3)
      GraphicsWindow.DrawLine(x1[level], y1[level], x2[level], y2[level])
      level = level + 1
      arm[level] = arm[level - 1] * 0.3
      If level <= maxlevel Then
        xc[level] = x1[level - 1]
        yc[level] = y1[level - 1]
        DrawIce() ' recursive call
        xc[level] = x2[level - 1]
        yc[level] = y2[level - 1]
        DrawIce() ' recursive call
      EndIf
      level = level - 1
    EndSub
    


    Nonki Takahashi

    Saturday, June 9, 2012 12:19 PM
    Moderator