0% found this document useful (0 votes)
19 views13 pages

TCL Procedures

The document provides an overview of procedures in Tcl, explaining their purpose as reusable code blocks that avoid code repetition. It details the syntax for defining procedures using the 'proc' command, examples of returning values, handling multiple and default arguments, and the distinction between local and global variables. Additionally, it introduces the 'upvar' command for accessing variables outside a procedure's context.

Uploaded by

abinava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

TCL Procedures

The document provides an overview of procedures in Tcl, explaining their purpose as reusable code blocks that avoid code repetition. It details the syntax for defining procedures using the 'proc' command, examples of returning values, handling multiple and default arguments, and the distinction between local and global variables. Additionally, it introduces the 'upvar' command for accessing variables outside a procedure's context.

Uploaded by

abinava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

TCL – Procedures

Dr. C. Prayline Rajabai


Assistant Professor Senior
School of Electronics Engineering
Vellore Institute of Technology
Overview
 Procedures are code blocks with series of commands.

 It provides a specific reusable functionality.

 Used to avoid same code being repeated in multiple locations.

 Procedures are equivalent to the functions used in many programming languages.

 In Tcl, this is done with the help of proc command

Syntax :

proc procedureName {arguments} {

body

2 VIT - SENSE 10-01-2024


Proc command
 Proc command has 3 arguments.

1. Name of the procedure

2. List of names of the arguments to the procedure

3. TCL script – that forms the body of the procedure.

Example :

proc helloWorld {} {

puts "Hello, World!"

helloWorld

When above code is executed, it produces following result.

Hello, World!

3 VIT - SENSE 10-01-2024


Return command - Example
 If you wish for a procedure to return early under certain circumstances without executing its
entire script, you can invoke the return command.

 It causes the enclosing procedure to return immediately.

Example :
proc fac x {
if {$x <= 1} {
return 1
}
expr $x * [fac [expr $x-1]]
}

fac 4 fac 0
⇒ 24 ⇒1
4 VIT - SENSE 10-01-2024
Procedures with multiple arguments
Example :

proc add {a b} {

return [expr $a+$b]

puts [add 10 30]

When above code is executed, it produces following result.

40

5 VIT - SENSE 10-01-2024


Procedures with default arguments
 Default values can be provided to the arguments, which can be used if no value is provided.

 It is sometimes referred as implicit arguments.

Example :

proc add {a {b 100} } {

return [expr $a+$b]

puts [add 10 30]; puts [add 10]

When above code is executed, it produces following result.

40

110

6 VIT - SENSE 10-01-2024


Procedures with variable arguments
Example :

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 above code is executed, it produces following result.

65

66 VIT - SENSE
7 10-01-2024
Local and global variables
 In the Tcl procedure body, all the variables assigned or passed are called local variables.

 They are only accessible within the procedure and are deleted when the procedure returns.

 Variables referenced outside any procedure are called global variables.

 A procedure can reference global variables with the global command using the following syntax.

Syntax : global var1 var2

 The global command treats each of its arguments as the name of a global variable.

 References to those names within the procedure will be directed to global variables instead of local ones.

8 VIT - SENSE 10-01-2024


Local and global variables
Example :

set accumulator “Hello”

proc accum {string} {

global accumulator

append accumulator $string \n

accum { World}

puts "$accumulator

When the above code is executed you will get the following result.

=> Hello World

9 VIT - SENSE 10-01-2024


Upvar command
 The upvar command provides a general mechanism for accessing variables outside the context of a

procedure.

 It can be used to access either global variables or local variables in some other active procedure.

 The first argument to upvar is the name of a variable accessible to the procedure’s caller.

 The second argument is the name of a local variable.

10 VIT - SENSE 10-01-2024


upvar command
Example (To print the contents of an array):

proc printarray name {

upvar $name a

foreach el [lsort [array names a]] {

puts "$el = $a($el)"

set info(age) 37; set info(position) "Vice President"

printarray info

When the above code is executed you will get the following result.

age = 37

position = "Vice President"


11 VIT - SENSE 10-01-2024
12 VIT - SENSE 10-01-2024
13 VIT - SENSE 10-01-2024

You might also like