Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


7.4 Multiple Return Values

So far we've only seen functions which return only one value, since this is something common to most programming languages. However, E allows you to return up to three values from a function. To do this you list the values separated by commas after the ENDPROC, RETURN or IS keyword, where you would normally have specified only one value. A good example is a function which manipulates a screen coordinate, which is a pair of values: the x- and y-coordinates.

PROC movediag(x, y) IS x+8, y+4

All this function does is add 8 to the x-coordinate and 4 to the y-coordinate. To get to the return values other than the first one you must use a multiple-assignment statement:

PROC main()
  DEF a, b
  a, b:=movediag(10, 3)
  /* Now a should be 10+8, and b should be 3+4 */
  WriteF('a is \d, b is \d\n', a, b)
ENDPROC

a is assigned the first return value and b is assigned the second. You don't need to use all the return values from a function, so the assignment in the example above could have assigned only to a (in which case it would not be a multiple-assignment anymore). A multiple-assignment makes sense only if the right-hand side is a function call, so don't expect things like the following example to set b properly:

  a,b:=6+movediag(10,3)  -> No obvious value for b

If you use a function with more than one return value in any other expression (i.e., something which is not the right-hand side of an assignment), then only the first return value is used. For this reason the return values of a function have special names: the first return value is called the regular value of the function, and the other values are the optional values.

PROC main()
  DEF a, b
  /* The next two lines ignore the second return value */
  a:=movediag(10, 3)
  WriteF('x-coord of movediag(21, 4) is \d\n', movediag(21,4))
ENDPROC


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.