Tcl procedure

A procedure is a command that is created by proc.

For example, info args expects its argument to be the name of a procedure. If it is given the name of a command that is not a procedure, it will return an error. info body also expects its argument to be the name of a procedure.

Some programming languages draw a distinction between a procedure or subroutine, and a function. When such a distinction is made, a function is usually more similar to a mathematical function -- a map from its inputs to a value. In Pascal, for example, a procedure does not return a value, and can not be called from a point in code where a value is expected, whereas a function returns a value and can be called from such a point.

In Perl, a subroutine returns a value and can be used in both roles. Python does the same, but chose the term "function" instead of "subroutine". In both of those languages, as well as in most traditional imperative languages, a statement serves as the framework for calling procedures and functions, and manipulating their results. Tcl unifies all three concepts into one single concept: The command. This is what makes control structures in Tcl so flexible. They cannot be implemented as part of the syntax of a statement because Tcl has no such thing. It only has commands.

Functional languages also unify the three concepts, "procedure", "function", and "statement" into the concept of a "function". It's this common unification that makes functional programming feasible in Tcl.

The language of expr avoids the concept a "statement" by not providing any syntax to allow evaluation of multiple expressions in a single call to expr. It also provides a functions, with syntax that is more similar to standard mathematical notation. expr functions are also surfaced as commands in the tcl::mathfunc namespace, illusting that a command is the more general concept. Any Tcl command can be exposed as an expr function, and such functions are not limited to the mathematical sense of mapping inputs to a value, but retain the generic power of any Tcl command.

See Also

Pass by reference
Implicit upvar