:TITLE: Variations in proc arguments and return values
;#
;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl12.lsn,v 1.1 2004/11/04 16:01:13 davidw Exp $
;# Copyright (c) 1995 Clif Flynt
;# 9300 Fleming Rd.
;# Dexter, MI 48130
;# clif@cflynt.com
;# See file "NOTICE" for licensing terms.
;#
:LESSON_TEXT_START_LEVEL 0:
A proc can be defined with a set number of required arguments (as was
done with sum in the previous lesson, or it can have a
variable number of arguments. An argument can also be defined to have a
default value.
Variables can be defined with a default value by placing the variable name and the default within braces within args. See the example code to see how this is done.
A proc will accept a variable number of arguments if the last declared argument is the word args. If the last argument to a proc argument list is args, then any arguments that aren't already assigned to previous variables will be assigned to args.
Example is defined with three arguments. At least one argument *must* be present when example is called. The second argument can be left out, and in that case it will default to an empty string. By declaring args as the last argument example can take a variable number of arguments.
The value that the body of a proc returns can be defined with the
return command. The return command will
return its argument to the calling program. If there is no return, then
body will return to the caller when the last closing brace has
been reached, and the proc will return the value of the last command to
be executed.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 1:
A proc can be defined with a set number of required arguments (as was
done with sum in the previous lesson, or it can have a
variable number of arguments. An argument can also be defined to have a
default value.
Variables can be defined with a default value by placing the variable name and the default within braces within args. See the example code to see how this is done.
A proc will accept a variable number of arguments if the last declared argument is the word args. If the last argument to a proc argument list is args, then any arguments that aren't already assigned to previous variables will be assigned to args.
Example is defined with three arguments. At least one argument *must* be present when example is called. The second argument can be left out, and in that case it will default to an empty string. By declaring args as the last argument example can take a variable number of arguments.
Note that if there is a variable other than args after a variable with a default, then the default will never be used. For example, if you declare a proc as
proc function { a {b 1} c} {...}
Tcl assigns values
to a proc's variables in the order that they are listed in the command. If
you provide 2 arguments when you call function they will be
assigned to a and b, and Tcl will generate an
error because c is undefined.
You can, however, declare other arguments that may not have values as coming after an argument with a default value. For example, this is valid:
proc example {required {default1 a} {default2 b} args} {...}
In this case, example requires one argument, which will
be assigned to the variable required. If there are two
arguments, the second arg will be assigned to default1.
If there are 3 arguments, the first will be assigned to required,
the second to default1, and the third to default2.
If example is called with more than 3 arguments, all the arguments
after the third will be assigned to args.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 2:
A proc can be defined with a set number of required arguments (as was
done with sum in the previous lesson, or it can have a
variable number of arguments. An argument can also be defined to have a
default value.
The variable declared in args may also be a list. This is done by enclosing two strings within braces. When the Tcl interpreter encounters this construct, it declares the variable's name to be the first string, and the default value to be the second string. If the proc is called without an argument in that position, then the default value will be assigned to the variable.
A proc can defined to accept a variable number of arguments by using the args argument. If the last argument to a proc argument list is args, then any arguments that aren't already assigned to previous variables will be assigned to args.
Example is defined with three arguments. At least one argument *must* be present when example is called. The second argument can be left out, and in that case it will default to an empty string. By declaring args as the last argument example can take a variable number of arguments.
Note that if there is a variable other than args after a variable with a default, then the default will never be used. Consider a proc declared as:
proc function { a {b 1} c} {...}
Tcl assigns values
to a proc's variables in the order that they are listed in the command. If
you provide 2 arguments when you call function they will be
assigned to a and b, and Tcl will generate an
error because c is undefined.
You can, however, declare other arguments that may not have values as coming after an argument with a default value. For example, this is valid:
proc example {required {default1 a} {default2 b} args} {...}
In this case, example requires one argument, which will
be assigned to the variable required. If there are two
arguments, the second arg will be assigned to default1.
If there are 3 arguments, the first will be assigned to required,
the second to default1, and the third to default2.
If example is called with more than 3 arguments, all the arguments
after the third will be assigned to args.
As you see in the example, with args as the last argument,
example can be called with one, two, three or more arguments.
If you call example with one arg, then second is set to
the default, and args is an empty list. When example is
called with two arguments, then first is set to the first argument,
second is set to the second argument, and args is still an
empty list. If there are more then two arguments, then the third - Nth
arguments are assigned to args
:TEXT_END:
:CODE_START:
proc example {first {second ""} args} {
if {$second == ""} {
puts "There is only one argument and it is: $first";
return 1;
} else {
if {$args == ""} {
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"
:TEXT_END: