Writing procedures in TCL

tcl
Galvin Verghese picture Galvin Verghese · Mar 14, 2011 · Viewed 18.3k times · Source

I am very new for TCL. Just I want to know that how to write TCL procedures without argument and how to call and how to execute it.

Answer

Donal Fellows picture Donal Fellows · Mar 14, 2011

To write a procedure that doesn't take any arguments, do this:

proc someName {} {
    # The {} above means a list of zero formal arguments
    puts "Hello from inside someName"
}

To call that procedure, just write its name:

someName

If it was returning a value:

proc example2 {} {
    return "some arbitrary value"
}

Then you'd do something with that returned value by enclosing the call in square brackets and using that where you want the value used:

set someVariable [example2]

To execute it... depends what you mean. I assume you mean doing so from outside a Tcl program. That's done by making the whole script (e.g., theScript.tcl) define the procedure and do the call, like this:

proc example3 {} {
    return "The quick brown fox"
}
puts [example3]

That would then be run something like this:

tclsh8.5 theScript.tcl