开发者

Writing procedures in TCL

开发者 https://www.devze.com 2023-02-16 12:44 出处:网络
I am very 开发者_StackOverflow中文版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. To write a procedure that doesn\'t take an

I am very 开发者_StackOverflow中文版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.


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


You can define a procedure like this:

proc hello_world_proc {} {
  puts "Hello world"
}

And you can execute it by simply writing:

hello_world_proc

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

# Procedure declaration
proc hello_world_proc2 {} {
  return "Hello world"
}

# Procedure call
puts [hello_world_proc2]


proc myProc {} {
    # do something
}

# call proc
myProc


Te official Tcl website has some documentation on functions (procedures) that could help you at https://www.tcl.tk/man/tcl/TclCmd/proc.htm.

Procedure with no argument

If you don't need any argument here is how to write the procedure you want:

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

And you can call it as follows:

funcNameNoArgs

Procedure with arguments

Now let's say you need arguments in the future. Here is the way to write that precedure in TCL:

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

You can call that function by doing:

funcName arg1 arg2 arg3

Here is a piece of code for you to try!

Remember to define functions before you call them, or you will get an error.

Try to copy paste this code in 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"


Syntax of procedure

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

Let See the Example:

  1. Without Arguments:

    proc Hello_eg { } { puts "Hello I M In procedure" }
    

How to run:

step 1: write tclsh on prompt

step 2: write the procedure as per above mention

step 3: write just the procedure name (i.e Hello_eg) to run 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 prompt

step 2: write the procedure as per above mention

step 3: write just the procedure name with arguments (i.e Hello_Arg Ramakant Singla) to run the procedure


It's pretty simple.

Defining :

proc myproc {} {

}

calling :

myproc 

Since you are New, I advise you to go through tutorial point. They have simple and consolidated content.


Procedure is a set of statements which is being preapeated in a program.

Syntax

proc <Name> {INPUTS} {
BODY
}

Eg:

proc add {m n} {
    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 procedure we have provide a name (add) to the set of statements which can be call in the main program.


Any amount of arguments

What maybe would come in handy is using args.
By using args you can pass any amount of arguments to your procedure.

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

Optional Arguments

Another tip: Enclosing arguments with { } makes 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 Example 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    


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

It'll have a chapter on Subroutines that'll help you define a TCL proc without and with arguments.

Regards Sharad


To create a TCL procedure without any parameter you should use the proc keyword followed by the procedure name 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 created procedure by simply calling its name: hello_world


This solution is based on previous questions about writing procs. I personally feel this is one of the better ways to write a procedure in tcl.

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

精彩评论

暂无评论...
验证码 换一张
取 消