Procedures are blocks of code that contain a sequence of commands, offering specific reusable functionality. They help avoid redundant code across multiple locations.
Procedures in Tcl are akin to functions in various programming languages. In Tcl, this is achieved using the proc
command. Here’s the syntax:
proc procedureName {arguments} {
body
}
The proc
command accepts three arguments:
proc greetWorld {} {
puts "Hello World!"
}
greetWorld
Hello World!
To exit a procedure early without completing its entire script, use the return
command. It causes the enclosing procedure to return immediately.
proc factorial n {
if {$n <= 1} {
return 1
}
expr $n * [factorial [expr $n - 1]]
}
puts [factorial 4]
puts [factorial 0]
24
1
proc add {x y} {
return [expr $x + $y]
}
puts [add 10 30]
40
Default arguments provide default values when no value is supplied.
proc add {a {b 100}} {
return [expr $a + $b]
}
puts [add 10 30]
puts [add 10]
40
110
proc average {numbers} {
set total 0
foreach num $numbers {
set total [expr $total + $num]
}
set avg [expr $total / [llength $numbers]]
return $avg
}
puts [average {70 80 50 60}]
puts [average {70 80 50}]
65
66
In the body of a Tcl procedure, variables assigned or passed are called local variables, accessible only within the procedure. Variables referenced outside any procedure are global variables.
To reference global variables within a procedure, use the global
command.
proc accumulate {str} {
global accumulator
append accumulator $str "\n"
}
The upvar
command provides a mechanism for accessing variables outside the context of a procedure.
proc printArray name {
upvar $name arr
foreach element [lsort [array names arr]] {
puts "$element = $arr($element)"
}
}
set info(age) 37
set info(position) "Vice President"
printArray info
age = 37
position = "Vice President"
Errors in Tcl commands can occur for various reasons, such as nonexistent commands, incorrect number of arguments, or malformed arguments.
set list {44 16 123 98 57}
set total 0
foreach el $list {
set total [expr $total + $element]
}
The script will be aborted with the error message: can’t read "element": no such variable.
Catch
Command
The catch
command is used to continue executing a script even after an error occurs. It takes two arguments: the script to execute and a variable to hold the error or result.
set message
catch {unset x} message
puts $message
can’t unset "x": no such variable
Exceptions in Tcl include errors, as well as the break
, continue
, and return
commands, which cause work in progress to be aborted.
catch {return "all done"} result
puts $result
all done
proc Div {a b} {
if {$b == 0} {
error "Error generated by error" "Info String for error" 401
} else {
return [expr $a / $b]
}
}
if {[catch {puts "Result = [Div 10 0]"} errmsg]} {
puts "ErrorMsg: $errmsg"
puts "ErrorCode: $errorCode"
puts "ErrorInfo:\n$errorInfo\n"
}
if {[catch {puts "Result = [Div 10 2]"} errmsg]} {
puts "ErrorMsg: $errmsg"
puts "ErrorCode: $errorCode"
puts "ErrorInfo:\n$errorInfo\n"
}
ErrorMsg: Error generated by error
ErrorCode: 401
ErrorInfo:
Info String for error
(procedure "Div" line 1)
invoked from within
"Div 10 0"
Result = 5