Chapter VI: Functions and Subroutines

In MBSL as in many languages, code is organized into groups called functions. A function takes data as parameters, then performs a specific task using the provided data. This function takes an hour and returns "AM" if it is in the morning and "PM" if it is in the afternoon:

Function AMPM(Hour as Long) as String
   If Hour < 12 Then
      AMPM = "AM"
   Else
      AMPM = "PM"
   End If
End Function

We call the function by using the name and parameters:

Dim Time as Long

Time = GetHour()
PlayerMessage(Player, "The time is " + Str(Time) + ":00" + AMPM(Time), Yellow)

Function and Sub

There is one primary difference between functions and subroutines. Functions return a value, and subroutines don't.

The Function Header

Value and Reference

This is a more advanced topic. It will probably be moved under UDTs.
(Note to self: Primitive types are passed by value and UDTs are passed by reference. How Java-esque! Except String is a primitive..)

RunScript

« Back to Chapter V: Loops
Table of Contents
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.
Hosted by www.Geocities.ws

1