Lesson 11:proc application (with default parameters)
1.//all parameter values are not necessarily entered when using the process. The input parameters of the procedure can have default values. The default value is determined by
{Default parameter name default} specified. If the procedure is called without specifying the values of these parameters, its default value is used.
Otherwise, use the input values to override the default values. When using default parameters, be aware that if the default parameters are followed by a
Non-default parameter, the value of the default parameter also requires input when this procedure is called, or an error occurs. This is because
When calling a procedure for TCL, the parameters and input values are matched according to the location.
2. If the last parameter in the argument list is args, the procedure can receive a variable number of input parameters. When a call has been
Parameter values other than the specified parameters are received by args. If only the args item is in the argument list, the args connection
Accept all input parameter values
Example①: The use of the return value of the calling function
Proc Test {a {b 7} {str "Hello World"}} {
Puts "$str"
return [expr $a * $b]
}
Puts "[Test 7 7]"
Example②:
Proc Test {args} {
Puts "input values are: $args"
}
Test my name is Zhouli
Result:input values Are:my name is Zhouli
To verify the beginning of the 2nd Word.
Example③:
Proc Config_sys {args} {
Array Set Inarr $args
Parray Inarr
}
Config_sys-sysname "HUB100"-ipaddr 192.168.10.1-date 2003-11-21-time 21:03:45
Result:
Inarr (-date) = 2003-11-21
Inarr (-ipaddr) = 192.168.10.1
Inarr (-sysname) = HUB100
Inarr (-time) = 21:03:45
Put it on first.
Go to this lesson!! Cough cough
Proc Example {first {second ""} args} {
if {$second = = ""} {
Puts "There is only one argument and it is: $first";
return 1;
} else {
if {$args = = ""} {
Puts "There is arguments-$first and $second";
return 2;
} else {
Puts "There is many arguments-$first and $second and $args";
return "many";
}
}
}
Set COUNT1 [Example one]
Set Count2 [Example one]
Set COUNT3 [Example one, three]
Set COUNT4 [Example one, three four]
Examples combine a lot of simple usages, not explanations
Tcl Script Learning 11: Proc Application (with default parameters)