Tcl proc Command
last modified April 3, 2025
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.
Procedures must be defined before use.
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 must come after required ones.
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
must be
the last parameter.
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 to avoid infinite loops.
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.
This tutorial covered the Tcl proc
command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.