Tcl Tutorial Lesson 11

Adding new commands to Tcl - proc

In Tcl there is actually no distinction between commands (often known as 'functions' in other languages) and "syntax". There are no reserved words (like if and while) as exist in C, Java, Python, Perl, etc... When the Tcl interpreter starts up there is a list of known commands that the interpreter uses to parse a line. These commands include while, for, set, puts, and so on. They are, however, still just regular Tcl commands that obey the same syntax rules as all Tcl commands, both built-in, and those that you create yourself with the proc command.

The proc command creates a new command. The syntax for the proc command is:

   proc name arguments body

When proc is evaluated, it creates a new command with name name that takes arguments arguments. When the procedure name is called, it then runs the code contained in body.

arguments is a list of arguments which will be passed to name. When name is invoked, local variables with these names will be created, and the values to be passed to name will be copied to the local variables.

The value that the body of a proc returns can be defined with the return command. The return command will return its argument to the calling program. If there is no return, then body will return to the caller when the last of its commands has been executed. The return value of the last command becomes the return value of the procedure.


Example

proc sum {arg1 arg2} {
    set x [expr {$arg1 + $arg2}];
    return $x
}

puts " The sum of 2 + 3 is: [sum 2 3]\n\n"

  Resulting output
 The sum of 2 + 3 is: 5

If you make a mistake in the call, Tcl will write an error message:

puts " The sum of 2 + 3 is: [sum 2]\n\n"

gives:

wrong # args: should be "sum arg1 arg2"
    while executing
"sum 2"
    invoked from within
"puts " The sum of 2 + 3 is: [sum 2]\n\n""
    (file "xx.tcl" line 6)

Advanced usage

As an aside, it is possible to redefine any builtin command or procedure. This is useful for instance for debugging purposes, although there are other means for this as well. As a simple demonstration:

proc for {a b c} {
    puts "The for command has been replaced by a puts";
    puts "The arguments were: $a\n$b\n$c\n"
}

for {set i 1} {$i < 10} {incr i}