Microsoft Small Basic

Program Listing:

Embed this in your website

'Implementing the recursive method for Factorial and Fibonacci''Iterate is Human, Recursion is Divine''This example helps to understand recursion and management 'of parameters and return a result in the calling of functions 'and in general the working mechanism of the memory heap''In General'Every time we have to call a function which can overwrite our 'local variable, we have to push them into a stack. If the function 'needs parameters, after the local variable, we push into the stack 'the parameters. After a function call, we have to remove all the 'element from the Stack. If the function return a value we need a 'pop to get that value, and then a pop for each local variable we saved.'In the called function we have to pop the parameters and finally 'push the resulting value. ''In this example n is a local variabile into Main(), Fact() and Fib()'subroutines but I never lost the local value. In addition the subroutine 'Fact() and Fib() accept a parameter and it returns a value''One final note ... I hate "Goto"... I love the structured programming... Small Basic is very cute'Main - User InterfaceSubMain'Main Programn=1'An initial value for nsel=1'An initial value for selWhile(sel<>0)TextWindow.WriteLine("0) Exit")'A menuTextWindow.WriteLine("1) Factorial")TextWindow.WriteLine("2) Fobonacci")TextWindow.Write("Make your choice: ")sel=TextWindow.ReadNumber()'What would you like to do? If(sel>0Andsel<3)Then'When I have a good choiceTextWindow.Write("Insert a number: ")n=TextWindow.ReadNumber()'Which number? EndIfStack.PushValue("Heap",n)'Save local variable nStack.PushValue("Heap",n)'Set the paramenter for the Fact() or Fib() functionIf(sel=1)Then'Calculating factorial Fact()ElseIf(sel=2)Then'Calculating Fibonacci Fib()'Even if no function will be executed, push e pop are rightEndIf'and the Stack will be cleared!ret=Stack.PopValue("Heap")'Get the returned value n=Stack.PopValue("Heap")'Restore local variable TextWindow.WriteLine("")If(sel=0)ThenTextWindow.WriteLine("Exiting...")ElseIf(sel=1)Then'Factorial result TextWindow.WriteLine(n+"! = "+ret)ElseIf(sel=2)Then'Fibonacci resultTextWindow.WriteLine("Fib("+n+")="+ret)Else'Wrong ChoiceTextWindow.WriteLine("Wrong choice!")EndIfTextWindow.WriteLine("")TextWindow.WriteLine("")EndWhileEndSub

'Fact - Factorial calculatingSubFactn=Stack.PopValue("Heap")'Get ParameterIf(n<0)Then'The Parameter must be positiveTextWindow.WriteLine("There is an error, the number is negative!")Stack.PushValue("Heap",0)ElseIf(n=0)Then'n! where n=0 is 1Stack.PushValue("Heap",1)'Set the return value to 1ElseStack.PushValue("Heap",n)'Save Local Variable Stack.PushValue("Heap",n-1)'Set Parameter for new Fact() callFact()'Recursion for (n-1)!'Set return value with n * (n-1)!Stack.PushValue("Heap",Stack.PopValue("Heap")*Stack.PopValue("Heap"))EndIfEndSub

'Fib - Fibonacci calculatingSubFibn=Stack.PopValue("Heap")'Get parameterIf(n=0)ThenStack.PushValue("Heap",0)'Return value for n=0ElseIf(n=1)ThenStack.PushValue("Heap",1)'Return value for n=1ElseStack.PushValue("Heap",n)'Save local variable nStack.PushValue("Heap",n-1)'Set Parameter for Fib()Fib()'Recursion... WOW! Get Fib(n-1)ret=Stack.PopValue("Heap")'Get Resultn=Stack.PopValue("Heap")'Restore local variable n Stack.PushValue("Heap",ret)'Save local Variable Ret, n is no more necessaryStack.PushValue("Heap",n-2)'Set Parameter for Fib()Fib()'Recursion...I'm so glad. Get Fib(n-2)'Return value Fib(n-1)+Fib(n-2)Stack.PushValue("Heap",Stack.PopValue("Heap")+Stack.PopValue("Heap"))EndIfEndSub