﻿ Introducing ThinBASIC > Example 3
 Navigation:  Introducing ThinBASIC > Example 3

A little more complex script example.

2 functions, one standard, one recursive.

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

Dim msg   As String

Dim count As Long

Dim N1   As Ext

Dim N2   As Ext

'---

' A simple standard function

'---

Function Factorial(InVal As Number) As Ext

Dim i As Ext

Dim r As Ext

r = 1

For i = 2 To InVal

r = r * i

Next

Function = r

End Function

'---

' A recursive function

'---

Function RecursiveExample(n As Number, MaxRecurse As Number) As EXT

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

INCR n

If n >= MaxRecurse Then

Function = n

Exit Function

Else

Function = RecursiveExample(n, MaxRecurse)

End If

End Function

'---

'---Main Program

'---

N1 = 10

N2 = Factorial(N1)

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

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

Dim count2 As Long

For count = 1 To 64

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

If MOD(count,2) = 0 Then

msg = msg & crlf

Else

msg = msg + " "

End If

For count2 = 1 To 100

count2 = count2

Next

Next

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