next up previous
Next: Control flow Up: An introduction to Previous: Substitutions

Procedures

An example of a simple procedure:

% proc sayhi {} { 
        puts "hello world!" 
        puts "testing 1 2 3"
}
Yes, that's it. proc means that we are going to define a new procedure. Next is the name of procedure (sayhi in this case), after that the names of the parameters ({} means there are no parameters) and at last the procedure body. sayhi can now be used like any other command:
% sayhi
hello world!
testing 1 2 3

A procedure with some parameters:

% proc myproc {var1 var2} {
        puts "var1 is var1"
        puts "var2 is var2"
}
% myproc hello world
var1 is hello
var2 is world

A procedure returning a value:

% proc multiply {var1 var2} {
        return [ expr var1 * var2 ]
}
% puts [ multiply 3 27 ]
81

All parameters are passed by value and can't be changed.



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996