Monday, December 7, 2015

procs

procedures are supported by TCL :

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:
proc proc_name { formal parameters } {
  statement1
  statement2
  :
  return
}
Example:
proc plus { a b } {
  expr $a + $b
  return
}

Syntax:
proc name argList body
where,
name - is the procedure name
argList - List of arguments. If a procedure takes more than one argument, they should be specified within braces.
body - body of the procedure
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 varName ?varName ...?
where
varName - is the name of variable whose scope has to be
extended beyond a procedure. More than one variable could be 
specified.
"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:

Example 2: Simple tree traversal iterative procedure.

Example 3 :

No comments: