return value from tcl script

Return immediately from the current procedure or script, with the specified result. The default result is the empty string.

When there is no return in a script, its value is the value of the last command evaluated in the script.


#!/usr/bin/tclsh

proc example {first {second ""} args} {
    if {$second eq ""} {
        puts "There is only one argument and it is: $first"
        return 1
    } else {
        if {$args eq ""} {
            puts "There are two arguments - $first and $second"
            return 2
        } else {
            puts "There are many arguments - $first and $second and $args"
            return "many"
        }
    }
}

set count1 [example ONE]
set count2 [example ONE TWO]
set count3 [example ONE TWO THREE ]
set count4 [example ONE TWO THREE FOUR]

puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"


Output :
There is only one argument and it is: ONE
There are two arguments - ONE and TWO
There are many arguments - ONE and TWO and THREE
There are many arguments - ONE and TWO and THREE FOUR
The example was called with 1, 2, many, and many Arguments

No comments:

Post a Comment