The Basics of Programming

Subroutines

Programs can get very big as more and more lines of instructions are added. Also, in some cases you may need to re-use bits of code over and over again. This is where subroutines and functions come in.

A subroutine is a set of instructions that does a specific task, for example, you could have a subroutine that calculates the average of an array of numbers and displays the result. A subroutine will always have a name, and sometimes provided with a list of values (known as parameters) that it can use, in this case an array and the number of items in the array (called num). A subroutine never returns a value, We will use a counted loop, called a for loop, which will count through the array from 1 to num and use a variable called sum to store the sum of the values, and another variable called avg to store the average. The print command will display the result on the computer screen.

E.g.

SUB average ( numbers(), num)

  sum = 0 ' Set sum of numbers to zero 

  FOR n=1 TO num  ' Count from 1 to num, and increase count by 1 
    sum = sum + numbers(n)
  NEXT N
  avg = sum / num
  PRINT  "The average is ";avg 

END SUB
To use this subroutine, you can call it from the main program or even other subroutines, using its name and pass it the variables or values that it needs.

e.g.

DIM numbers(5) ' Define array of five integers, including one for index 0 
numbers(1) = 456
numbers(2) = 172
numbers(3) = 78
numbers(4) = 102
numbers(5) = 321
CALL average( numbers(), 5)

and it should display the following result:

The average is 225.8.

The old way of calling subroutines is to use GOSUB label or line number, and then use the RETURN statement to return to the main program.

e.g.
...
GOSUB CalcAvg
PRINT average
...
END
CalcAvg:
average = total / num
RETURN

Functions

A function is very similar to a subroutine, it is still a set of instructions that does a specific task, but this time it will return a value, the type of value is specified by the variable type e.g. LONGINT,ADDRESS, SHORTINT,SINGLE or STRING. To return a value, you use assign the function name a value. Then the function also can be used in expressions, so if you have a complex calculation to perform regularly, you can create a function and and use it in expressions and assignments.

e.g.

SUB SINGLE Average (numbers(), num) ' Function is called average and returns a floating point number
  sum = 0 ' Set sum of numbers to zero
  FOR n=1 TO num
    sum = sum + numbers(n)
  NEXT n
  avg = sum / num;
  Average = avg ' Assign return value to the name of the function
END SUB

Now you can use it in expressions or assignments from the main program, subroutines or even other functions.
e.g.

MyAverage = Average(numbers(), 5)
PRINT "The average is "; MyAverage

Many programming languages have built in libraries of functions which you can use in your programs. There are functions that do complex arithmatic such as square root, sines, cosines, tangents, minimum, maximum values. There are functions that are performed on strings such as length of a string, substrings, concatenate strings and so on.

Next Page