Procedures are nothing
but code blocks with series of commands that provide a specific reusable
functionality. It is used to avoid same code being repeated in multiple
locations.
Procedures are
equivalent to the functions used in many programming languages and are made
available in Tcl with the help of proc command.
The syntax of creating
a simple procedure is shown below −
proc
procedureName {arguments} {
body
}
Procedure provides following
benefits
Easy
maintenance – changes needs to be made only at one place
Saves
space, time and effort as a function is implemented only once
Procedures can be shared
when placed in “libraries”
A simple example for
procedure is given below –
proc
helloWorld {} {
puts "Hello, World!"
}
helloWorld
When the above code is
executed, it produces the following result −
Hello, World!
Procedures with Multiple
Arguments
An example for
procedure with arguments is shown below –
proc
add {a b} {
return [expr $a+$b]
}
puts
[add 10 30]
When the above code is
executed, it produces the following result –
40
Procedures with Variable
Arguments
An example for
procedure with arguments is shown below –
proc
avg {numbers} {
set sum 0
foreach number $numbers {
set sum [expr $sum + $number]
}
set average [expr $sum/[llength $numbers]]
return $average
}
puts
[avg {70 80 50 60}]
puts
[avg {70 80 50 }]
When the above code is
executed, it produces the following result –
65
66
Procedures with Default
Arguments
Default arguments are
used to provide default values that can be used if no value is provided. An
example for procedure with default arguments, which is sometimes referred as
implicit arguments is shown below –
proc
add {a {b 100} } {
return [expr $a+$b]
}
puts
[add 10 30]
puts
[add 10]
When the above code is executed, it produces the following result
−
40
110
Recursive Procedures
An example for
recursive procedures is shown below –
proc
factorial {number} {
if {$number <= 1} {
return 1
}
return [expr $number * [factorial [expr $number - 1]]]
}
puts
[factorial 3]
puts
[factorial 5]
When the above code is executed, it produces the following result
−
6
120
4 ways to pass an
arguments to procedure.
Pass by value
Proc sum {a b} {
Set num [expr $a + $b]
Puts “The sum is: $num”
}
Sum 2 3
Pass by name
Array set months {1 Jan 2 Feb}
Parray months
Or
Proc increase {initial_apy change} {
Upvar $initial_pay x
Foreach item [array names x] {
Set x($item) [expr $x($item) + $change]
}
}
Array set Pay {Ray Steve 50 Fred 200}
Increase Pay 25
Parray Pay
Defaults
Proc myprocdefault {{}{}{}} {
Puts “$a $b $c”
}
Myprocdefault
Variable arguments
Proc show {a args} {
Puts $a
foreach val $args {puts $val}
}
Show 1 2 3
Create a procedure that
will accept one argument and variable arguments
proc variable {a args} {
puts $a
foreach val $args { puts $val }
}
variable 1 2 3 4 5
Output:
1
2
3
4
5
Example
Variable number of arguments.
proc
args_parser {args} {
set length [llength $args]
puts "Length of the args is $length"
for { set i 0 } { $i < $length } { incr i } {
set arg_name [lindex $args $i]
incr i
set arguments($arg_name) [lindex $args $i]
}
return [array get arguments]
}
proc
b {args} {
array set ar [eval args_parser $args]
set name $ar(-name)
set age $ar(-age)
set country $ar(-country)
set location $ar(-location)
puts "$name is aged $age\n";
puts "$name is located at $location,$country\n";
}
b
-name manish -age 27 -location bangalore -country India
No comments:
Post a Comment
Note: only a member of this blog may post a comment.