BTCL Procedures


One of the most useful features of BTCL version 95.0 is that you can quickly define and use your own procedures. Most BTCL procedures have the form:

procedure_name argument_1 argument_2 ... argument_n

The arguments here are positional and completely general, being distinguished only by separating spaces. You can create a procedure by using the proc command which takes three arguments:

proc procedure_name procedure_argument_list procedure_body
A simple procedure to add two numbers could be defined as:

BTCL >   proc add {a b} {expr $a+$b}
This procedure can then be used immediately by entering, for example:

BTCL >   add 3 4 
 7
If a procedure already exists with the name add, then the new procedure replaces it. For example, you could define your own version of the for command:

BTCL >   proc for {init test every body} { 
                 eval $init 
                 while $test {eval $body ; eval $every} 
         }
Note that this simulation of the for statement is not very good, since the break and continue statements are not processed, and variables inside the body variable are only local to this procedure (which might be desirable).

An example is the procedure annealed_dynamics, defined below, which could be used to repeatedly equilibrate a system (set up outside the procedure) over a range of target temperatures:

proc annealed_dynamics {temp_start temp_final iterations num_anneals} { 
        if {$num_anneals <= 0} return
        if {$num_anneals == 1} { 
                $T_start = $temp_final 
                $T_scale = 0
        } else { 
                $T_start = $temp_start 
                $T_scale = ($temp_final - $temp_start) / (num_anneals - 1) 
        }
        for {$i = 0} {$i < $num_anneals} {incr i} { 
                $temp = $T_start + $i * $T_scale 
                dynamics iteration = $iterations temperature = $temp 
        } 
}
The arguments to a procedure are read-only, and any variables changed or created (e.g., $i = 0) are local. However, global variables can be accessed by using the global declarator. You can also use the optional return statement to return a result that was not the last result generated. The following example shows these capabilities:

BTCL >   set world 0 ; set i 0 
 0
BTCL >   proc example {a b} { 
         global world 
         set i [expr $world*$a+$b] 
         incr world 1 
         return $i 
         }
BTCL >   example 3 4 
 4
BTCL >   example 3 4 
 7
BTCL >   echo $i 
 0
BTCL >   echo $world 
 2
Notice that here, the variables world and i were set at the global level, but only world is affected by the procedure, because of the global declarator.

It is also possible to create BTCL (TCL) procedures, like the echo command, that take any number of arguments. Since this requires that you know more about TCL's list-handling capabilities, please refer to the TCL documentation (Ousterhout 1994).


Main access page Advanced-Use access.

BTCL Language access Control Statements Commands and Utilities

Copyright Biosym/MSI