Written Procedures in TCL

I am very new to TCL. I just want to know how to write TCL procedures without arguments and how to call and how to execute them.

+8
tcl
source share
11 answers

To write a procedure that takes no arguments, do the following:

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

To call this procedure, simply write its name:

 someName 

If it returned a value:

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

Then you will do something with this return value, enclosing the call in square brackets and using this where you want to use the value:

 set someVariable [example2] 

To accomplish this ... depends on what you mean. I assume that you mean doing this outside of the Tcl program. This is done by creating an entire script (for example, theScript.tcl ) that defines the procedure and theScript.tcl call, for example:

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

Then something like this will execute:

 tclsh8.5 theScript.tcl 
+20
source share

You can define a procedure as follows:

 proc hello_world_proc {} { puts "Hello world" } 

And you can execute it by simply writing:

 hello_world_proc 

If you want to use the return value of the procedure, you can do:

 # Procedure declaration proc hello_world_proc2 {} { return "Hello world" } # Procedure call puts [hello_world_proc2] 
+5
source share
 proc myProc {} { # do something } # call proc myProc 
+3
source share

Procedure syntax

 proc <Name Of procedure> {No of arguments, if u want don't need simply left empty} { <Body> } 

See an example:

  • Without arguments:

     proc Hello_eg { } { puts "Hello IM In procedure" } 

How to start:

step 1: write tclsh on the command line

Step 2: write the procedure above

Step 3: write only the name of the procedure (e.g. Hello_eg) to start the procedure

2.With arguments:

 proc Hello_Arg { first second } { puts "The first argument is: $first" puts "The Second argument is: $second" } 

How to run this:

step 1: write tclsh on the command line

Step 2: write the procedure above

Step 3: just write the name of the procedure with arguments (e.g. Hello_Arg Ramakant Singla) to start the procedure

+2
source share

The official Tcl website has documentation of features (procedures) that can help you at https://www.tcl.tk/man/tcl/TclCmd/proc.htm .

No argument procedure

If you don’t need any argument, here’s how to write the desired procedure:

 proc funcNameNoArgs {} { puts "Hello from funcNameNoArgs" } 

And you can call it like this:

 funcNameNoArgs 

Argument Procedure

Now let me say that you need arguments in the future. Here's how to write this predicate to TCL:

 proc funcNameWithArgs {arg1 arg2 arg3} { puts "Hello from funcNameWithArgs " } 

You can call this function by doing:

 funcName arg1 arg2 arg3 

Here is a code snippet for you!

Remember to define the functions before calling them, or you will receive an error message.

Try copying this code into your interpreter to get started and play with it:

 proc funcNameNoArgs {} { puts "Hello from a function with no arguments" } funcNameNoArgs proc funcNameWithArgs {arg1 arg2 arg3} { puts "Hello from a function with 3 arguments" puts $arg1 puts $arg2 puts $arg3 } funcNameWithArgs "Argument 1" "Argument 2" "Argument 3" 
+2
source share

A procedure is a set of instructions that are previously created in a program.

Syntax

 proc <Name> {INPUTS} { BODY } 

For example:

 proc add {mn} { set s 0 set s [expr $m + $n] return $s } #Main Program Starts Here set x 2 set y 3 set Result [add $x $y] puts "$Result" 

In the above example .... in the procedure, we provided a name ( add ) for a set of statements that can be called in the main program.

+1
source share

Any number of arguments

What may come in handy, use args .
Using args , you can pass any number of arguments to your procedure.

 proc withAnyNumberOfArguments {args} { if {$args eq ""} { puts "got no arguments" } foreach arg $args { puts "got $arg" } } 

Additional Arguments

Another tip: nesting arguments with { } make them optional arguments.

 proc atLeastOneArgument {a1 {args}} { puts -nonewline "got a1=$a1" foreach arg $args { puts -nonewline " and $arg" } puts "." } 

Default values

If you want to have default values, you can specify them as follows:

 proc putsTime { {secondsSinceBeginOfEpoch "now"} } { if {$secondsSinceBeginOfEpoch eq "now"} { set secondsSinceBeginOfEpoch [clock seconds] } return [clock format $secondsSinceBeginOfEpoch] } 

Some sample calls

 1 % withAnyNumberOfArguments got no arguments 2 % withAnyNumberOfArguments one got one 3 % withAnyNumberOfArguments ready steady go! got ready got steady got go! 4 % atLeastOneArgument "this is one argument" ;# because its in double quotes got a1=this is one argument. 5 % atLeastOneArgument 3 2 1 go! got a1=3 and 2 and 1 and go!. 6 % puts [formatTime] Fri Dec 18 16:39:43 CET 2015 7 % puts [formatTime 0] Thu Jan 01 01:00:00 CET 1970 
+1
source share

In addition to the answers above, I would recommend using tcltutor.exe (available from http://tcltutor.software.informer.com/3.0b/ ) to find out TCL.

It will have a chapter of routines to help you define the TCL process with and without arguments.

Charade relationship

+1
source share

To create a TCL procedure without any parameters, you must use the proc keyword, followed by the name of the procedure, and then the scope of your procedure.

 proc hello_world {} { // Use puts to print your output in the terminal. // If your procedure return data use return keyword. } 

You can use the generated procedure by simply calling its name: hello_world

+1
source share

It is pretty simple.

Definition:

 proc myproc {} { } 

call:

 myproc 

Since you are a beginner, I advise you to pass the training point. They have simple and consolidated content.

+1
source share

This decision is based on previous questions about writing procs. I personally think this is one of the best ways to write a procedure in tcl.

the code

 proc sampleProc args { # Defaults array set options {-device router0 -ip "10.16.1.62"} # Read args array set options $args # Assign set device $options(-device) set ip $options(-ip) # Usage puts "Device under use is $device and IP is $ip" # Return return "${sd} :: $ip" } 

Execution

 tclsh> source sampleProc.tcl Device under use is router0 and IP is 10.16.1.62 router0 :: 10.16.1.62 
0
source share

All Articles