ANY-maze Help > The ANY-maze reference > The Protocol page > The elements of a protocol > Testing > Procedures > Re-using parts of procedures

Re-using parts of procedures

Introduction

Procedures can be flexible on what they can achieve in a test, but this means that they can become very complex. Often, a procedure will repeat certain code, or the same thing needs to be done by more than one procedure running as part of a test.

Details

To reduce procedure complexity, ANY-maze allows you to define sub-procedures which can be invoked from within any procedure. This allows you to keep Procedures shorter, and if you need to change any code that needs to be repeated, you only need to change it once (in the sub-procedure) and not multiple times.

You can share information between a Procedure and the sub-procedure(s) that it calls using variables; simply set up the variables to be shared between Procedures for a given test.

Example

As an example, let's consider a piece of apparatus that contains two arms; at the end of each arm is a pellet dispenser. When the animal approaches the end of the arm (a 'target zone'), a pellet will be dispensed. This will also cause either a light or a noise stimulus to be switched on (at random); this stimulus will only be switched off when the animal retreats back down the arm to an area at the start of the arm.

A procedure to do all this will be long and fairly complicated:

  

  

Figure 1. An example of a procedure that could benefit from the use of a sub-procedure.

So how can we reduce the complexity of this Procedure? It should be fairly obvious that the bulk of the code is repeated, i.e. the code that turns on the pellet dispenser, turns on the stimulus, and waits for the animal to retreat again before turning it off. The only difference between that repeated code is which pellet dispenser and stimulus need to be turned on.

The first thing we'll do is create three variables to note which pellet dispenser, light stimulus and noise stimulus should be used. These variables will be shared between procedures, which means that the main procedure can set them and the sub-procedure can then use them. Then, we simply need the main procedure to set these three variables before calling the sub-procedure. This leaves us with the following main procedure, which I'm sure you'll agree is much simpler!

  

  

Figure 2. The same procedure, reworked to call a sub-procedure.

That just leaves the matter of defining the sub-procedure. The first thing you'll need to do when creating the sub-procedure is check the box on the Procedure's settings page titled This procedure is a sub-procedure. This will mean that ANY-maze won't actually run this procedure, unless you call it directly from within another procedure. Then, the sub-procedure itself contains the repeated code from the main procedure, but instead of directly accessing the specific I/O (pellet dispenser, light stimulus and noise stimulus), we simply refer to the variables that the main procedure set for us:

  

  

Figure 3. The sub-procedure that is called from the main procedure.

As you can see, using sub-procedures can greatly reduce the code in a given procedure; in fact the same sub-procedure can be invoked from within more than one different procedure. Not only that, a sub-procedure allows you to separate key bits of functionality into easier-to-understand chunks, since there is less code in each procedure.

See also:

 Setting up a procedure 
 The Run sub-procedure statement  

© Copyright 2003-2026 Stoelting Co. All rights reserved

ANY-maze help topic T0466