Small Basic – Nesting Subroutines


This blog post continues what you learned in the Triangle Area Calculator, here:

Small Basic - The Triangle Area Calculator

Nesting Subroutines Overview

A subroutine might call other subroutines, like in Figure 1. In that figure, the main program calls a subroutine, SubA(), which in turn calls another subroutine, SubC(). Subroutines called from other subroutines are nested subroutines.

Figure 1: Illustrating nested subroutines

As an example of using nested subroutines, let’s build on your triangle area calculation program and add other geometric shapes. You’ll add the ability to find (1) the area of a square, where your user enters either its side or the length of its diagonal, and (2) the area of a circle, where your user enters either its radius or its circumference (perimeter). See Figure 2.

Figure 2: Two additional shapes supported by the area calculator program

The main program now changes to ask your user whether he wants to find the area of a triangle, a square, or a circle (see Figure 3). Each option is handled by its own subroutine which, as you’ll see in a moment, can call other subroutines to do the job. You'll need to make a main menu image like the one in Figure 3.

Figure 3: The Area Calculator program in action

The main program’s in Listing 1. Write this program out; the other listings in this section build on it! Save your file as "AreaCalculator".

Listing 1: Area Calculator

 1 ' AreaCalculator.sb

 2 ' Calculates the area of different geometric shapes

 3

 4 GraphicsWindow.Title  = "Area Calculator"

 5 TextWindow.Title      = GraphicsWindow.Title

 6 GraphicsWindow.Width  = 480

 7 GraphicsWindow.Height = 360

 8 GraphicsWindow.CanResize = 0

 9

10 GetChoice:

11 bkgndImg = Program.Directory + "\MainMenu.png"

12 GraphicsWindow.DrawImage(bkgndImg, 0, 0)

13

14 TextWindow.Write("Enter your choice [1-3]: ")

15 choice = TextWindow.ReadNumber()

16

17 If (choice = 1 ) Then

18   TriangleAreaManager()

19 ElseIf (choice = 2 ) Then

20   SquareAreaManager()

21 ElseIf (choice = 3 ) Then

22   CircleAreaManager()

23 Else

24   TextWindow.WriteLine("Sorry, that's not an option.")

25 EndIf

26

27 TextWindow.WriteLine("")

28 Goto GetChoice

In Line 11 of Listing 1, it displays the MainMenu.png image as the background image. You'll need to create an image like that and save it with the "MainMenu.png" name into the Small Basic files folder for your AreaCalculator.sb program.

As the program got bigger, you promoted yourself to a higher position (like a director) and hired three managers (the three subroutines) under you to handle the special functions of calculating the areas of triangles, squares, and circles (see Figure 4). Each one of these managers could have other subroutines working for him to perform the required functions. (Don’t talk to these subroutine managers in real life, or people will think you’re crazy; at least put your hand up to your ear so that people think you’re on the phone.)

Figure 4: Illustrating the Area Calculator subroutines

The TriangleArea.sb program (that you wrote back in Small Basic - The Triangle Area Calculator) has now become part of the TriangleAreaManager() subroutine in Listing 2. The three triangle-area-calculation subroutines are still the same as before, so we won’t show you them again (trust us, this blog post is long enough). So please go grab those three subroutines from Small Basic - The Triangle Area Calculator.

Listing 2: The TriangleAreaManager Subroutine

 1 Sub TriangleAreaManager

 2   bkgndImg = Program.Directory + "\TriMenu.png"

 3   GraphicsWindow.DrawImage(bkgndImg, 0, 0)

 4

 5   TextWindow.Write("Triangle choice [1-3]: ")

 6   choice = TextWindow.ReadNumber()

 7

 8   If (choice = 1 ) Then

 9     Tri1Area()

10   ElseIf (choice = 2 ) Then

11     Tri2Area()

12   ElseIf (choice = 3 ) Then

13     Tri3Area()

14   Else

15     TextWindow.WriteLine("Sorry, that’s not an option.")

16   EndIf

17 EndSub

Our SquareAreaManager is still young and energetic (he probably tweets a lot), and he wants to do all the work by himself, without any help from subroutines (see Listing 3).

Listing 3: SquareAreaManager Subroutine

 1 Sub SquareAreaManager

 2   bkgndImg = Program.Directory + "\SquareMenu.png"

 3  GraphicsWindow.DrawImage(bkgndImg, 0, 0)

 4

 5   TextWindow.Write("Square choice [1-2]: ")

 6   choice = TextWindow.ReadNumber()

 7

 8   If ( choice = 1 ) Then

 9     TextWindow.Write("Enter the side-length: ")

10     s = TextWindow.ReadNumber()

11     area = s * s

12     TextWindow.WriteLine("Area = " + area )

13   ElseIf (choice = 2 ) Then

14     TextWindow.Write("Enter the diagonal: ")

15     d = TextWindow.ReadNumber()

16     s = Math.SquareRoot(2) * d

17     area = s * s

18     TextWindow.WriteLine("Area = " + area )

19   Else

20     TextWindow.WriteLine("Sorry, that’s not an option.")

21   EndIf

22 EndSub

The subroutine starts by displaying the two available options (see Figure 5) and requesting your user to enter his choice (Lines 2-6). If he selects the first choice, the subroutine asks him to enter the side-length of the square, and then it calculates and displays the area (Lines 8-12). If he selects the second choice, the subroutine prompts him to enter the length of the square’s diagonal, and then it uses the entered value to find the side-length s and compute the area (Lines 13-18). If he enters a number that isn’t on the list, the subroutine tells him that his entered choice isn’t an option (Lines 19-20).

Figure 5: The Square Area Calculator menu

Right-click the Square Area Calculator menu image above and click Save As. Save the image as "SquareMenu.png" into your folder with the AreaCalculator.sb file.

Our CircleAreaManager decided to take a different approach. He isn’t good in math (he doesn’t like pi, but he loves pie), so he decided to hire a worker under him (the CircleAreaCalculator), and that worker calculates the two circle options (and she brought in a pi expert). The CircleAreaManager() subroutine is in Listing 4.

Listing 4: CircleAreaManager Subroutine

 1 Sub CircleAreaManager

 2   bkgndImg = Program.Directory + "\CircleMenu.png"

 3  GraphicsWindow.DrawImage(bkgndImg, 0, 0)

 4

 5   TextWindow.Write("Circle choice [1-2]: ")

 6   choice = TextWindow.ReadNumber()

 7

 8   If ( (choice = 1) Or (choice = 2) ) Then

 9    CircleAreaCalculator()

10   Else

11     TextWindow.WriteLine("Sorry, that’s not an option.")

12   EndIf

13 EndSub

14

15 Sub CircleAreaCalculator

16   If ( choice = 1 ) Then

17     TextWindow.Write("Enter the radius: ")

18     r = TextWindow.ReadNumber()

19   Else

20     TextWindow.Write("Enter the circumference: ")

21     c = TextWindow.ReadNumber()

22     r = c / (2 * Math.Pi )

23   EndIf

24

25   area = r * r * Math.Pi

26   TextWindow.WriteLine("Area = " + area )

27 EndSub

Like before, this subroutine displays the available choices (see Figure 6) to the user and waits for her input (Lines 2-6). If her choice is valid, Small Basic calls the CircleAreaCalculator() subroutine to do the calculation (Lines 8-9). If it isn’t valid, your program displays an error message, “Sorry, that’s not an option” (Lines 11-12).

Figure 6: The Circle Area Calculator menu

Right-click the Circle Area Calculator menu image above and click Save As. Save the image as "CircleMenu.png" into your folder with the AreaCalculator.sb file.

How does the CircleAreaCalculator() subroutine know what to do? Well, the top-level manger, the CircleAreaManager() subroutine, saves the type of calculation to be done (in the choice variable) before
making the call. It calls the CircleAreaCalculator() subroutine only if your user’s choice is valid (she picks 1 or 2). That’s why the CircleAreaCalculator subroutine uses a single If/Else statement.

Self-Check

Change the main program from this section to add a rectangle to the main menu. Add a RectangleAreaManager() subroutine for your user to enter either: (1) the length and width of the rectangle, or (2)the length of one side and the diagonal as we show below. (Note: you'll need to make some images like we did.)

Leave a comment if you have any questions!

Learn more about Small Basic: http://blogs.msdn.com/b/smallbasic/

Small and Basically yours,

   - Ninja Ed & Majed Marji

Skip to main content