Previous
Switch Loop 
Next
File Handling in Tcl/Tk 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Procedures

Procedures

In some languages they call it functions. In others they call it subroutines. Yet others call it methords. In TCL we call it Procedures. I know that there are subtle and humanly indistinguishable changes between those words - but who cares?

Syntax:
proc procedure_name arguments ?args? body

I like this syntax better...
proc procedure_name { arguments ?args? } {
body
}

The proc command creates a new Tcl procedure by any name, replacing any existing command or procedure there may have been by that name. Whenever the new command is invoked, the contents of body will be executed by the Tcl interpreter. Arguments can be specified to the procedure. It consists of a list, possibly empty, each of whose elements specifies one argument. Each argument specifier is also a list with either one or two fields. If there is only a single field in the specifier then it is the name of the argument; if there are two fields, then the first is the argument name and the second is its default value.

For this command I will give a C++ example and the TCL example...

In C++

int plus(int a, int b)
 {
 int ans=a+b;
 return ans;
 }
In TCL

proc plus {a b} {
set ans [expr $a+$b]
return $ans
}

The given arguments can be a number, a string, a list, a human being - in short, anything. I still have not understood how to give a human being as the argument - but I firmly belive that it is possible.

The defined functions can be called by using its name. The arguments are listed after the name. An example for using the above function.


#Function Plus
proc plus {a b} {
set ans [expr $a+$b]
return $ans
}

set sum [plus 1 2]
label .txt -text "Sum of 1 and 2 is $sum"
pack .txt
Previous
Switch Loop 
Next
File Handling in Tcl/Tk 

Comments

Guest at 30 Nov, 2007 02:45
A typo in this text:

set sum [plus 1 2]
label .txt -text "Sum of 1 and 2 is $sum"
pack.txt <===

____|_

There should be a space after pack and before '.txt'
Reply to this.
Binny V A at 17 Jan, 2008 12:48
Thanks - I have fixed that issue.
Reply to this.
Anonymous at 24 May, 2008 12:00
And, strickly speaking, these are called "commands" in tcl, although to be created with the "proc" command. They are "commands" everywhere in the documentation and the language (for example, give an "info commands" command to interpreter).
Reply to this.
Anonymous at 04 Jul, 2008 03:24
Really.. can a human being be send as an argument.. I have seen the other way around.. that is, human being sending arguments :)
Reply to this.
Anonymous at 11 Jun, 2009 01:03
Is it possible to declare a procedure in one file and import it for use in another? That is, is it possible to create a custom procedure library?
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed