Name

proc — Create a new procedure.

Synopsis

proc [name] arglist body

Description

The proc command creates new procedures, which are virtually indistinguishable from built-in Hecl commands. name is the name of the new command, or, if it is absent, an anonymous procedure is created (and should be stored in a variable). arglist is a list of arguments that the new command will take and make available as local variables within the body, which is the code executed every time the command is called. If the last element of the argument list is args, the variable args is a list that is filled with any arguments (including 0) above and beyond the number of arguments specified for the proc.

Example

proc addlist {lst} {
    set res 0
    foreach e $lst {
	incr $res $e
    }
    return $res
}

puts [addlist {1 2 3 4 5}]
	  

Produces:

15

args Example

proc showargs {args} {
    puts "Args: $args"
}
showargs
showargs x y z
	

Produces:

Args: 
Args: x y z

Anonymous proc Example

set foo [proc {x} { puts $x }]
$foo beebop
	

Produces:

beebop