Procedures return to the calling program through "return" statement
Every procedure must have a corresponding return statement. This however is not strictly required.
Once they are created, procedures could be used just as any other TCL command
Procedures are declared with the help of keyword "proc "
Each procedure may have a set of formal parameters and some variables - local and global
Syntax:
statement1
statement2
:
}
Example:
}
Syntax:
Example:
% proc proc_a { a b c } { body of procedure }
% proc proc_b b { body of procedure }
Once created, a procedure could be invoked/ called just by its name
Example: proc_a { $a $b $c } , proc_b { $b } w.r.t. The above example
Every procedure has its own set of local variables.
Any two procedures may have variables with the same names. The values of the variables do not get affected since they are not visible outside the procedure block.
"global " could be used to declare the scope of some variables as global
"global " should be used inside a procedure
Syntax:
"global " binds variable names "varName "... to global variables. References to these names will refer to global variables instead of local variables for the duration of the current procedure.
Example 1:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc proc_a { } { | |
global globvar | |
puts "Variable globvar is associated to $globvar" | |
set globvar "Variable updated inside proc_a" | |
puts $globvar | |
} | |
proc proc_b { } { | |
set globvar "Variable updated inside proc_b" | |
puts $globvar | |
} | |
set globvar "outside proc" | |
proc_a | |
puts "Value of globvar now after proc_a execution: $globvar" | |
proc_b | |
puts "Value of globvar now after proc_b execution: $globvar" | |
Example 2: Simple tree traversal iterative procedure.
Example 3 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set child(a) "b c" | |
set child(b) "d e" | |
set child(c) "f g h" | |
set child(d) "i j" | |
set child(e) "k l" | |
set child(f) "m" | |
set child(g) "n o p" | |
set child(h) "q" | |
set child(i) r | |
set child(j) s | |
set child(k) "t u" | |
proc iter_tree { a } { | |
global child; | |
if [info exists child($a)] { | |
foreach aa $child($a) { | |
puts "$aa is child of $a" | |
iter_tree $aa | |
} | |
} | |
} | |
iter_tree a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Procedure whose default variable is set 1 | |
#This script will increment the variable with the given amount of increment if provided. | |
#if optional increment value is not provided, it will increment by 1 | |
proc increment { var {increment 1} } { | |
return [expr $var+$increment] | |
} |
No comments:
Post a Comment