Subs And Functions

UNDER CONSTRUCTION!!!!!

Index.

What is SUB?

  • What it does.
  • How to use it.
  • What it does return.

What is FUNCTION.

  • What it does.
  • How to use it.
  • What does it return.

When i use SUB and when FUNCTION?

  • General principles of using these two.

What is SUB.

SUB is a subprogram. It is a good way to make your code easier to follow with out irritating GOTO jumps.
Basicly, SUB does something and then programs returns from where ever sub was originally called.

How to use it.

At first, you need to declare your sub. After that, your program knows there does exist this sub.
Here is a simple example of a sub wich does print same text everytime you call it.

' At first, we need to declare your sub. Note that there ain't no parameters between ()
Declare Sub MySub()

' Then we code that sub.
SUB MySub()
    Print "This comes from a sub"
END SUB

' From here, the program actually starts.
CLS
Randomize Timer
DIM AS Integer Counter

For Counter = 1 TO INT(RND * 10) + 1
    MySub
Next Counter

END

Output is This comes from a sub for a one to ten times, depends what INT(RND) returns.

Using parameters with a sub.
If you want to use parameters with a sub, it's easy task. Let's now think we need to print out something else, something user input is.

' We declare that sub.
Declare MySub(Temp AS String)

' Now we code our sub. Note that there is stuff between ()
SUB MySub(Temp AS String)
    Print Temp
END SUB

' From here, the program actually starts.
CLS
Randomize Timer
DIM AS Integer Counter
DIM AS String UserInput

INPUT "What is the text"; UserInput

For Counter = 1 TO INT(RND * 10) + 1
    MySub(UserInput)
Next Counter

END
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.