Procedures 

One of the most significant enhancements to the original Basic language has been the addition of procedures and functions.  These can greatly simplify programming by breaking programs into smaller logical components.

 Basic18 supports types of procedures 

  1. Sub procedures or procedures that do not return a value
  2. Function procedures or procedures that return one or more values

*** Program execution always starts with the main procedure

Therefore all programs must have a Sub main() with no parameters

 

Defining Sub Procedures

A Sub Procedure is defined using the Sub and End Sub keywords

Sub procedurename(variable list)

  statements

End Sub

Each time the procedure is called, the statements between Sub and End Sub are executed.  Syntax for the variable list is the same as the Dim statement.

variable list =  varname As type

Each variable in the variable list can be separated by a comma

For example:

Sub test1()                      ' no parameters passed
Sub test2(x As uByte)            ' one uByte as a parameter
Sub test3(x As uByte, y As Fixed) ' two parameters a uByte and a Fixed

 

For all variable types except strings, the value of the variable is passed to the procedure.  For strings, a reference to the string data is passed to the procedure.  Therefore when you manipulate the value of a parameter (as long as it isn’t a string) the value of the variable in the calling procedure is not changed.
 

Using Sub Procedures

 Sub Procedures are used with the Call statement.

Call procedurename(variable list)

For example:

Sub main()
  Dim j as uByte
 
  Call init()  ' call the init procedure without parameters
  j=portd
  Call move2B(j) ' call the move2B procedure with 1 parameter
 
End Sub 

Sub init()  ' initialize hardware
  trisb=0: trisd=255
End Sub 

Sub move2B(x As uByte)
  portb=x
End Sub

 

User Functions 

Functions are special procedures that return one or more values.  The syntax is the same as a sub procedure except you change the word Sub to Function, and add a variable list to the End Function keyword.

Function functionName(parameter list)

  statements

End Function variable list

The big difference here is the variable list in the "End Function" statement.  This can contain a single variable, or a comma-separated list of variables.

The following function will add two Integers, and return the result as an Integer

Function Add(v1 as Integer, v2 as Integer)
  Dim res as Integer
 
  res=v1+v2
End Function res ' Return the value of res 

This next example will return both the sum and difference of two Bytes.  The sum will be returned as an Integer, and the difference will be returned as a Byte.

Function SumDiff(v1 As Byte, v2 As Byte)
  Dim sum as Integer, diff As Byte
  sum=v1+v2
  diff=v1-v2
End Function sum, diff  ' return both results

 

Using Functions 

Functions can be called in one of three different ways

  1. Using the call command
  2. Inside an expression
  3. As a multi variable assignment

 

Calling Functions

A function can be called using the Call statement, however any return values are ignored.

 

Functions in Expressions

When using a function inside an  expression, only the first value returned will be used.  For example, using our SumDiff function defined above, a is the sum of variables b and c +15

a=SumDiff(b,c)+15

 

Multi Variable Assignments

Once again using the SumDiff function we will demonstrate returning multiple variables.  The syntax is as follows:

variable list = function(parameter list)

x,y = SumDiff(b,c)

Here x =b+c and y=b-c

The variables are assigned from left to right.  If the destination list (in this case x,y) does not have as many variables as the return list, the right most variables are ignored.

Variables of the same variable type can be assigned literal values in the same statement as follows:

a,b,c = 25