Please enable JavaScript to view this site.

thinBasic Help Manual

Navigation: Introducing ThinBASIC

Example 3 - functions

Scroll Prev Top Next More

* * HELP & MANUAL PREMIUM PACK 4 DEMO VERSION * *

This documentation was styled with a demo skin from the Premium Pack 4 add-on for Help & Manual. The contents of the skin are encrypted and not configurable. You can only publish HM projects with this skin. You cannot edit it or change it.

This version is copyright and may only be used for local testing purposes. It may not be distributed.

Please purchase the full version of the Premium Pack to get the configurable skins and remove this notice. The package will also include the Toolbox configuration utility for Premium Pack skins.

 

A little more complex script example.

2 functions, one standard, one recursive.

 

'----------------------------------------------------------------------

 

' Optional TBMain function - if found, it is the first function to be executed

function TBMain()

 ' Traditional BASIC variable declaration

 dim msg   as string

 dim count as long
 

 ' Optional modern BASIC variable declaration (DATATYPE variable) with initialization

 ext N1 = 10

 ext N2 = Factorial(N1)

 

 MsgBox 0, "Factorial of " & N1 & " = " & Format$(N2, "0#")

 MsgBox 0, "I've called a recursive function " & RecursiveExample(1, N1) & " times"

 

 For count = 1 To 64

   msg = msg & "[" & Format$(2^count) & "]"

  If MOD(count,2) = 0 Then

     msg = msg & $crlf

  Else

     msg = msg & " "

  End If    

 Next

 

 MsgBox 0, "From 2^1 to 2 ^ 64:\n" & msg

end function

 

 

'---

' A simple standard function - note it can be declared after the line it is called on

'---

function Factorial(InVal As Number) As Ext

Dim i, r As Ext

 

 r = 1

For i = 2 To InVal

   r = r * i

Next

return r

 

end Function

 

'---

' A recursive function

'---

function RecursiveExample(n As Number, MaxRecurse As Number) As Ext

Dim s As String = Repeat$(10000, "X")

 

INCR n                                          

If n >= MaxRecurse Then                            

  return n                                    

Else                                            

  return RecursiveExample(n, MaxRecurse)      

End If                

 

end Function

 

 

 

Created with Help+Manual 8 and styled with Premium Pack Version 4 © by EC Software