Multiple Assignement
Tuple
A tuple is not an object nor an expression, it is a construct of the language
A tuple is a list of variables separated by commas et not enclosed by anything.
A tuple of expression may follows up a return keyword.
It may be assigned by a function call, providing the function returns also
a tuple, having the same number of elements. It may be assigned to another
tuple. A tuple of several variable may be assigned a single expression, in
this case, each variable of the tuple is assigned this expression.
Assigning tuples
There is two manners to perform multiple assignement. In the first case,
by initializing several variables with one value.
Example:
x, y, z = 0
The group of variables x, y, z is named a tuple. In this case, the three variables
x, y, z are initialized with the same zero value. But we can also assign several
values to several variables at once.
Example: x, y, z = 1, 2, 3
Here, 1 is assigned to x, 2 to y and 3 to z. In this second case, the number
of targets must match the number of values, otherwise an error message is
thrown by the compiler.
Multiple assignment is especially useful to allow a function returning several
values at once.
For example, we have a function named getColor() which returns the red, green,
blue components of a color, and we can write:
int red, green, blue
red, green, blue = getColor()
The compiler knows that the getColor() function has been defined to return
three values, and thus that this matches the number of variables. If the function
was defined to return a single value, the variables will be all assigned with
this same value. If the number of values is greater than one, and different
of the number of variables at left, an error message is thrown.
Unlike multiple declaration, a multiple assignment may be performed on variables
having different types.
Example:
int x
text t
x, t = 1000, "abc"
Multiple assignment may be used with the elements of an array too.
Arrays have a dynamic size and thus, the number of targets don't need to match
the number of elements, and if the array has a single element, only the first
variable will be assigned.
Example:
x, y, z = array(1, 2, 3, 4)
is equivalent to x = 1, y = 2, z = 3.
x, y, z = array(1)
is equivalent to x = 1 and other variables are not assigned.
Example of multiple assignement | int
x text t number n x, t, n = 10, "demo", 33 print x, t, n |
Displays: | > 10 demo 33 |
This is not a multiple assignement but
a multiple declaration. |
int
x, y, z = 10 print x, y, z `could be written int x =10, y = 10, z = 10 |
Displays: | > 0 0 10 |
Multiple assignement multiple with an unique source. | int,
int, int getRGB(int
color) int r = getRed(color) int r = getRed(color) int r = getRed(color) return r, g, b int r, g, b r; g; b = getRGB(0xFFFFFF) print r, b, b |
Displays: | > 255 255 255 |
Multiple assignement multiple from a unique value | int
power(int value) return
value * value int a, b a, b = power(10) print a, b |
Displays: | > 100 100 |
Exercises |
1) Here is a function returning two values. Declare and assign
the a and b variables a et b with the values returned by the function.. text, number afunction() text t = "demo" number n = 1234 return t, n Answer |