Home Online Manual
Back: Behavior of continue
Forward: First index is 1
FastBack: System dependent limitations
FastForward: Miscellaneous oddities
Up: Major differences to the C programming language
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

6.3.7 Return type of procedures

Although the SINGULAR language is a strongly typed programming language, the type of the return value of a procedure does not need to be specified. As a consequence, the return type of a procedure may vary, i.e., may, for example, depend on the input. However, the return value of such a procedure may then only be assigned to a variable of type def.

proc type_return (int i)
  if (i > 0) {return (i);}
  else {return (list(i));}
def t1 = type_return(1);
def t2 = type_return(-1);
typeof(t1); typeof(t2);
==> int
==> list

Furthermore, it is mandatory to assign the return value of a procedure to a variable of type def, if a procedure changes the current ring using the keepring command (see keepring) and returns a ring-dependent value (like a polynomial or module).

proc def_return
  ring r=0,(x,y),dp;
  poly p = x;
  keepring r;
  return (x);
def p = def_return();
// poly p = def_return(); would be WRONG!!!
==> poly

On the other hand, more than one value can be returned by a single return statement. For example,

proc tworeturn () { return (1,2); }
int i,j = tworeturn();