Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing procedures in TCL

Tags:

tcl

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.

like image 773
Galvin Verghese Avatar asked Mar 14 '11 10:03

Galvin Verghese


People also ask

What is procedure in Tcl?

Procedures are nothing but code blocks with series of commands that provide a specific reusable functionality. It is used to avoid same code being repeated in multiple locations. Procedures are equivalent to the functions used in many programming languages and are made available in Tcl with the help of proc command.

How do I run a procedure in Tcl?

We can call the procedure with one and two arguments. The second argument b , has an implicit value 2. If we provide only one argument, the power procedure then returns the value of a to the power 2. We call the power procedure with one and two arguments.

What does Proc mean in Tcl?

The proc command creates a new Tcl procedure named 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.

How do I write to a file in Tcl?

Puts command is used to write to an open file.


1 Answers

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
like image 164
Donal Fellows Avatar answered Nov 02 '22 23:11

Donal Fellows