Tuesday, June 12, 2012

TCL - 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 args body
When proc is evaluated, it creates a new command with name name that takes arguments args. When the procedurename is called, it then runs the code contained in body.
Args 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 }
2 + 3 is: [sum 2 3]\n\n" proc for {a b c
puts " The sum o f} {
uts "The for command has been replaced by a puts";
pputs "The arguments were: $a\n$b\n$c\n" }
for {set i 1} {$i < 10} {incr i}

No comments:

Post a Comment

Popular Posts