# Small Basic Subroutines – Input and Output

Subroutine Input and Output

In many cases, you might use your subroutine to create some working variables (or temporary variables) before you can get your final information. Although these working variables aren’t made to be used by the main program, the main program can still see them. A subroutine might also accidentally change a variable that belongs to the main program. If you’re not careful, this can cause problems. For an example of this, write out the program in the Listing.

Listing: Name Clash

' Shows the risk of using global variables. A temporary variable in a subroutine overwrites a variable in main.

r1 = 10 ' Radius of a sphere

r2 = 5  ' Radius of a cylinder

h = 5   ' Height of a cylinder

SphereVolume()

TextWindow.WriteLine("Sphere volume = " + V1)

CylinderVolume()

TextWindow.WriteLine("Cylinder volume = " + V2)

Sub SphereVolume

h = r1 * r1 * r1  ' Saves r1 cubed in a temporary variable

V1 = (4/3) * Math.Pi * h

EndSub

Sub CylinderVolume

V2 = Math.Pi * r2 * r2 * h

EndSub

Output:

Sphere volume =
4188.7902047863866666666666666

Cylinder volume = 78539.81633974475000

This program calculates the volume of a sphere (with the radius r1) and the volume of a cylinder (with the radius r2 and height h). The SphereVolume() subroutine uses a temporary variable (also named h) to save the result of a temporary calculation. So when the main program calls this SphereVolume() subroutine, the subroutine changes the value of the variable h. Now, when the main program calls the CylinderVolume() subroutine, it passes a wrong value for the cylinder height, h; the result is a wrong cylinder’s volume. The Figure shows you how the main program passes variables to and from multiple subroutines.

Figure: The main program passing variables to and from subroutines

Choosing Variable Names

You won’t have problems with name clashing if you carefully choose your variable names and if you use consistent naming. For example, you can start the names of all your temporary variables with an underscore. Another practice is to use the subroutine’s name (or part of the name) in the variable name (like SphereVolume_h instead of just h). But you could end up with long variable names that make your program hard to read. It’s up to you what variable names you use, and there are no strict rules you have to follow. But if you name your variables carefully, you (most likely) won’t cause a problem by accidentally using the same variable name more than once (or using it in the wrong way).

Small and Basically yours,

- Majed Marji & Ninja Ed

Tags

1. Finished formatting this blog post.

2. Computers Today (part 1 of 6)

blogs.msdn.com/…/computers-today.aspx

…..

CS SPOTLIGHT: Girls in computer programming… why it matters!!!

blogs.msdn.com/…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

Computational Thinking – Videos & Papers by Jeannette Wing

blogs.msdn.com/…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx