BBC BASIC 2.31 for the Tatung Einstein, showing DRIFT, the text adventure the course builds, in the middle of a game
← Back to Courses
module
17

Naming Your Own Commands

Introduction

As a program grows, it becomes hard to see what it does. Two hundred lines of PRINT and IF all look alike. BBC BASIC lets you take a piece of a program, give it a name, and use it by that name wherever you need it. The rest of the program then reads like a list of what it does: PROCTITLE, PROCLOOK, PROCMOVE. This is the most useful thing in the course, and DRIFT is built out of it.

A Procedure

100 DEF PROCTITLE
110 PRINT "*** DRIFT ***"
120 ENDPROC

DEF PROCTITLE starts the definition of a procedure called TITLE. Everything down to ENDPROC is its body. Nothing happens when the program reaches these lines by itself - see below - but anywhere in the program,

PROCTITLE

jumps to line 110, does the body, and at ENDPROC comes back and carries on from where it was. Every procedure's name starts with PROC, with no space: PROC TITLE is a Bad call.

Handing It Something

A procedure can be given values to work with:

200 DEF PROCROOM(NAME$)
210 PRINT "YOU ARE IN THE ";NAME$
220 ENDPROC

PROCROOM("BRIDGE") puts BRIDGE into NAME$ and runs the body. Call it again with "CRYO BAY" and the same lines print something different.

NAME$ here is a parameter, and it belongs to the procedure alone. If the program has a NAME$ of its own, calling PROCROOM does not touch it. A procedure can take several, separated by commas, and must be given exactly as many as it has - and in brackets. Leave the brackets off, PROCROOM, and BBC BASIC looks for a different procedure, one with nothing in brackets, and says No such FN/PROC.

Keeping Its Own Variables

Inside a procedure,

LOCAL COUNT

makes a COUNT that belongs to the procedure: it starts at 0 each time, and any COUNT outside is left alone. Anything not made LOCAL is shared with the whole program - which is sometimes exactly what you want, when a procedure's job is to change the score, say.

A Function

A function works something out and hands back the answer:

300 DEF FNDOUBLE(N)=N*2

FNDOUBLE(21) is 42, and can go anywhere a number can: PRINT FNDOUBLE(21), X=FNDOUBLE(X)+1. The names start with FN. A string function ends in $, FNUPPER$(A$), and hands back a string.

A function that needs more than one line ends with a line starting =:

300 DEF FNMAX(A,B)
310 IF A>B THEN =A
320 =B

=A hands back A and ends the function there and then.

Where They Go

Definitions go at the end, after an END. A program runs from the top, line after line. If it runs off the end of the main part into a DEF, BBC BASIC skips the DEF line itself but runs the body, reaches ENDPROC, and stops with No PROC - it was not called, so there is nowhere to return to. END stops the main program before that can happen.

The Code

10 PROCTITLE
20 PROCROOM("CRYO BAY")
30 PROCROOM("BRIDGE")
40 PRINT FNDOUBLE(21)
50 END
100 DEF PROCTITLE
110 PRINT "*** DRIFT ***"
120 ENDPROC
200 DEF PROCROOM(NAME$)
210 PRINT "YOU ARE IN THE ";NAME$
220 ENDPROC
300 DEF FNDOUBLE(N)=N*2

Starting from

A fresh boot, or NEW.

What you should see

>RUN
*** DRIFT ***
YOU ARE IN THE CRYO BAY
YOU ARE IN THE BRIDGE
        42
>

The first five lines are the whole program, and they read like a description of it.

Procedures and a function

Change One Thing

  • Delete line 50, the END, and run it. What is printed after 42, and what does BBC BASIC say?
  • Put END back, and change line 30 to 30 PROCROOM, with no brackets. What is the message?
  • Add 5 NAME$="MINE" and 45 PRINT NAME$. The procedure put BRIDGE in a NAME$ - so what does line 45 print, and why?

Exercises

17.1 Write PROCLINE(N), which prints a line of N stars, and use it twice with different numbers.

17.2 Write FNAREA(W,H), which gives the area of a W by H rectangle.

17.3 Write FNMAX(A,B), which gives the bigger of two numbers.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
No PROC at line ...The program ran on into a definition. Put END before the DEFs. (Or an ENDPROC with no DEF PROC above it.)
No such FN/PROCThe name is misspelt, the DEF is missing - or the brackets are left off a procedure that has parameters.
ArgumentsThe wrong number of values in the brackets.
Bad callA space between PROC and the name.
A variable changed when you did not expect itThe procedure changed it - it was not a parameter or LOCAL.
No PROC or No FN naming a DEF line, from a program that looked rightAn ENDPROC or = inside a FOR or REPEAT loop. A procedure or function cannot be left from inside a loop: let the loop finish, then leave. S22 shows the way round it.

Summary

DEF PROCNAME(...) ... ENDPROC defines a procedure, and PROCNAME(...) runs it. DEF FNNAME(...)=... defines a function that hands back a value. Parameters and LOCAL variables belong to the procedure alone. Definitions go after END.

Next

Part two begins: S18, Taking A Sentence Apart - pulling the words out of what somebody typed.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.