Tcl proc Command
last modified October 20, 2026
The Tcl proc command creates new procedures (functions) in Tcl.
Procedures help organize code into reusable blocks. They can accept
parameters and return values.
Basic Definition
The proc command defines a procedure with a name, parameter list,
and body. Procedures can be called like built-in Tcl commands. They help
modularize code.
Syntax: proc name params body. The name is the procedure
name. params is the parameter list. body contains
the procedure's code.
Simple Procedure
This example shows a basic procedure that takes no arguments and prints a message.
proc greet {} {
puts "Hello, Tcl!"
}
greet
The greet procedure is defined with empty parameter list {}.
When called, it executes its body and prints the greeting message.
A procedure must be defined before it is called, and its variables are local
to each call.
Procedure with Parameters
Procedures can accept parameters which become variables in the procedure body.
proc greet_name {name} {
puts "Hello, $name!"
}
greet_name "Alice"
greet_name "Bob"
This procedure takes one parameter name. When called, the
argument value is assigned to this parameter. The procedure can then
use this value in its body.
Returning Values
Procedures can return values using the return command. The
return value can be captured by the caller.
proc square {x} {
return [expr {$x * $x}]
}
set result [square 5]
puts "5 squared is $result"
The square procedure calculates the square of its input.
It returns the result which is captured in the result
variable. Procedures return the result of their last command if no
explicit return is given.
Default Parameter Values
Parameters can have default values which are used when arguments are not provided.
proc greet_optional {{name "Guest"}} {
puts "Hello, $name!"
}
greet_optional
greet_optional "Charlie"
The name parameter has a default value "Guest". When called
without arguments, the default is used. With an argument, it overrides
the default. Default parameters should come after the required ones; if a
parameter with a default is followed by a required one, both must be supplied
and the default is never used.
Variable Number of Arguments
Procedures can accept a variable number of arguments using the args
special parameter.
proc sum_numbers {args} {
set total 0
foreach num $args {
set total [expr {$total + $num}]
}
return $total
}
puts "Sum: [sum_numbers 1 2 3 4 5]"
The sum_numbers procedure accepts any number of arguments.
These are collected in the args list. The procedure then
iterates through them to calculate the sum. args is special only
as the last parameter; anywhere else it is an ordinary parameter name.
Recursive Procedures
Procedures can call themselves recursively to solve problems that can be broken down into smaller subproblems.
proc factorial {n} {
if {$n <= 1} {
return 1
} else {
return [expr {$n * [factorial [expr {$n - 1}]]}]
}
}
puts "5! = [factorial 5]"
This recursive procedure calculates factorials. It calls itself with a smaller value until reaching the base case (n ≤ 1). Recursion requires proper termination conditions: a procedure that never stops calling itself ends with a "too many nested evaluations" error once the recursion limit (1000 by default) is reached.
Best Practices
- Naming: Use descriptive procedure names that indicate purpose.
- Size: Keep procedures small and focused on single tasks.
- Documentation: Add comments explaining complex procedures.
- Parameters: Limit the number of parameters for clarity.
- Error Handling: Validate inputs and handle errors gracefully.
- Scope: Variables of a procedure are local; use arguments and return values, and reach outside variables with
global,upvaror a namespace only when needed.
This tutorial covered the Tcl proc command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.