Skip to content

Declaring Functions & Subroutines

CrossBasic supports two kinds of callable routines:

  • Function: Returns a value.
  • Subroutine (or Sub): Does not return a value.

Syntax

Function FunctionName(parameterList) As ReturnType
    ' …body…
End Function

Sub SubName(parameterList)
    ' …body…
End Sub
  • The Function keyword introduces a routine that must end with End Function and specify an As return type.
  • The Sub keyword introduces a routine that must end with End Sub and does not have a return type.
  • Both names are case-insensitive and follow the same naming rules as variables (letters, digits, underscores; must not start with a digit).

Examples

Simple Function

Module Foo
  Function Bar() As Integer
    Return 1
  End Function
End Module

This defines a function Bar that takes no parameters and returns the integer 1.

Subroutine

Sub PrintGreeting()
  Print("Hello, CrossBasic!")
End Sub

This defines a subroutine PrintGreeting that prints a message and returns no value.

Assigns-style “Function” for L-value syntax

CrossBasic additionally supports the Assigns keyword to make functions act like l-values:

Sub ChangeValue(a As Integer, b As Integer, Assigns c As Integer)
  Print("a=" + Str(a) + "  b=" + Str(b) + "  c=" + Str(c))
End Sub

ChangeValue(5, 4) = 10