Suite101

Writing PROCedures in BASIC

How to create named blocks of callable code using PROC and ENDPROC

© Guy Lecky-Thompson

Using blocks of code, called procedures in BBC BASIC programming is a little different from C, Visual Basic, and other languages. They are, however, powerful and useful.

Introduction

There are several ways to create blocks of code that can be called from anywhere in a program. Some are named, some not. Some can be passed parameters, other methods do not allow this. In BASIC, the most common forms are the PROCedure (PROC), FuNction (FN), and subrouting (GOSUB).

For those just starting out with programming, the differences between these kinds of constructs are outlined in the Procedure, Subroutine or Function? article.

Overview of PROCedures

A PROC, or PROCedure (or procedure) is a piece of named code that can be called from within the main BASIC code body. It can have parameters passed to it, these parameters can even be modified within the PROCedure, but it can not return a value. The general form for a PROCedure definition is:

DEF PROC_Proecure_Name(parameter_list ...)
 REM Code to do the work
ENDPROC

To call the PROCedure, we simply use a call similar to:

100 PROC_Procedure_Name(parameter_1)

PROCedures must be defined at the end of the program, unless they are single-line. The reason for this is that the program is executed in a top-down fashion, starting at the first line number, and proceeding until the END keyword is encountered, or there are no more lines to process.

There should be as many parameters in the call to the PROCedure as there are defined for the PROCedure, and they must be of the same type. If either condition is not met then an execution error will occur.

If we were to mix PROCedure definitions in with the BASIC program statements, there is a risk that the PROCedure would be executed out of sequence. Consequently, we usually define them in a special section, as follows:

1000 REM Code to do work up to here
1010 END
1020 :
1030 REM Define PROCedures from here on

If the last PROCedure does not have an ENDPROC, then an error will occur. Each PROC must pair up with an ENDPROC, but since the ENDPROC is not named, there is some scope for error if an ENDPROC is left out by accident. Correct code layout will help to avoid this.

There are a few conventions that we can follow with regards to naming our PROCedures.

Naming Conventions

In BBC BASIC, names of PROCedures, variables, functions and so on must follow a set of rules. For example, they can not contain space characters. They can, however, contain underscore characters ('_'). Consider the following:

DEF PROCGETUSERNAME
DEF PROC_GETUSERNAME
DEF PROC_Get_User_Name

The last example is the most readable, and hence the preferred way to name a PROCedure. Of course, we could also lose the capitalization, and write the definition as:

DEF PROC_get_user_name

The important point is to use a convention that makes it easy to write the code, whilst respecting the rules imposed by BBC BASIC. Remember that the PROCedure is designed to be called multiple times, so if writing get_user_name makes more sense to the programmer than Get_User_Name, then the first should be used in preference to the second.

A final point is that capitals are usually avoided (as in GET_USER_NAME, or GETUSERNAME) in order to avoid conflict or confusion with built-in functions and PROCedures.

Passing by Value

The default behavior is to pass all parameters by value, in other words, the variable (think pigeon hole or mailbox : see this article) is replaced at execution time with the actual constant value that it contains. Of course, for each call to the PROCedure, this value will likely change.

Passing by value means two things:

  • The value can not be changed in the PROCedure;
  • The value will be retained upon exit from the PROCedure

Of course, BBC BASIC provides a mechanism by which the programmer can indicate that the contents of the variable should be updateable by the PROCedure - pass by reference. There is one exception - arrays are always passed by reference.

Passing by Reference

When we pass by reference in BBC BASIC, the PROCedure is passed a notional reference to the variable. Through the reference, it is possible to update the value contained in the variable. In BBC BASIC, we use the RETURN keyword to indicate that a variable should be passed in this way:

DEF PROC_get_user_name(RETURN szUserName$)

Note that we have affixed the $ sign to the end of the variable name to indicate that a string type is expected here. If we were to call the PROCedure with the wrong type, whether it is passed by value or reference, an execution error will occur.

Return Values

The PROCedure mechanism does not allow the programmer to return a value from the PROCedure directly; only as a parameter passed to the PROCedure by reference. Consequently, if we were to define a mathematical PROCedure to perform some calculation, we would need to write code such as:

100 PROC_Calculate(my_value%)
110 new_value% = my_value% + another_value%
...
200 END
210 :
220 DEF PROC_Calculate(RETURN the_value%)
230  the_value% = the_value% * 42
240 ENDPROC

In order to return a value from a piece of named code (with or without parameters), we use a FuNction, or FN. These are discussed in a separate article.

Links


The copyright of the article Writing PROCedures in BASIC in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Writing PROCedures in BASIC in print or online must be granted by the author in writing.





Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo