TCL Notes9
TCL Notes9
Procedures can be used to simplify code and make it more modular and reusable. They
are an important part of the TCL language and are used extensively in TCL
programming.
1. Modular programming: Procedures allow you to break up your code into smaller,
more manageable pieces. This makes your code easier to read, understand, and
maintain.
2. Code reusability: Once you define a procedure, you can call it multiple times
from different parts of your code, without having to write the same code again and
again. This saves time and reduces the risk of errors.
3. Parameterization: Procedures can take input parameters, which allows you to pass
in different values and customize the behavior of the procedure based on those
values. This makes your code more flexible and adaptable.
proc show {} {
puts "hello world!"
}
show
proc show {} {
set a "welcome to bangalore"
return $a
}
puts [show]
puts [sum 1 2 3 4 5]
puts [sum 1 2 3 4]
puts [sum 1 2 3]
puts [sum 1 2]
puts [sum 1]
Arguments with default values/implicit arguments.
Implicit arguments in TCL procedures may have implicit values.
An Implicit arguments value is used if no explicit value is provided.
proc show {a {b 2} } {
puts $a
puts $b
}
show 10 20
puts [add 2]
proc arti {a b} {
set m1 [expr $a + $b]
set m2 [expr $a - $b]
set m3 [expr $a * $b]
set m4 [expr $a / $b]
return [list $m1 $m2 $m3 $m4]
}
local variables:
A varible decleared inside a procedure has a procedure scope/local varible.
proc show {} {
set a 10
puts $a
}
puts $a
show
global varibles:
Any variable which is declared outside the proc. we can
also use this varibale inside the proc.
set b 20
set b 20
puts [sumnum 5]
proc outer_proc {} {
proc inner_proc {name} {
puts "Hello, $name!"
}
inner_proc "Bob"
}
outer_proc
In Tcl, an array is a data structure that stores a collection of values, which can
be accessed using an index. It is similar to a list or a dictionary in other
programming languages. An array is defined using the "array" command, followed by
the name of the array and its values.
Array in TCL:
the follwing are some of the operators that can be used in TCL
arrays.
puts $colour($item)
unset colour(1)
parray colour
example:1
./file.tcl 1
example:2
./file.tcl std_name sub
the follwing are some of the operators that can be used in TCL
arrays.
puts $colour($item)
unset colour(1)
parray colour
example:1
tclsh p.tcl 2
example:2
tclsh p.tcl std_name sub