|
From: | Andreas Fischlin |
Subject: | Re: [Gm2] Returning a string constant |
Date: | Sun, 26 Oct 2008 15:26:18 +0100 |
User-agent: | Thunderbird 2.0.0.17 (Macintosh/20080914) |
Dear Scott, In standard Modula-2 yes, this is illegal. Wirth (1983) writes on p. 52: "Only one value, however, can be returned as the result of a function. This value, moreover, cannot be of a structured type." Please consider the following three arguments: (i) Functional procedures may be used in expressions, and using a string in an _expression_ doesn't make much sense. (ii) You have plenty of alternatives: Simply use procedure parameters and there is no need to return an object such as a string from a functional procedure (see alternative code provided below). (iii) Code does not look very good and it is a bad habit from C/Unix programmers to always use functional procedures and then to return the actual results as a side effect from the call to the procedure. In a easy to read programing style returning complex data from a procedure is best documented by using a procedure that is not a functional procedure and returns all the data through procedure parameters. Considering this your example can be rewritten as: MODULE test1; FROM InOut IMPORT WriteString, WriteLn; FROM Strings IMPORT Assign; TYPE testresult = ARRAY [0..9] OF CHAR; PROCEDURE test(VAR s: ARRAY OF CHAR); BEGIN (* s := "test"; may be accepted by some compilers, but is not recommended, since not safe. It is better to use Strings.Assign *) Assign("test",s); (* there is no need to have an explicit RETURN statement here *) END test; BEGIN test(testresult); WriteString('Test results = "'); WriteString(testresult); WriteString('"'); WriteLn; END test1. Hope this helps and answers your question. Regards, Andreas Cited references: -------------------- Wirth, N., 1983 (2 (corr.) ed.). Programming in Modula-2. Springer-Verlag: Berlin a.o, 176 pp. Scott Robinson wrote: Hello all, --
________________________________________________________________________ Dr. Andreas Fischlin, Ph.D., Group Director Terrestrial Systems Ecology Institute of Integrative Biology: Ecology, Evolution, Infectious Disease Department of Environmental Sciences, ETH Zurich Address: ETH Zurich, CHN E21.1 8092 Zurich, Switzerland Phone: +41 44 633-6090 / Fax: +41 44 633-1136 http://www.sysecol.ethz.ch/Staff/af/ http://www.sysecol.ethz.ch/ _/_/_/ _/_/_/ _/ _/ _/ _/ _/ _/ Eidgenoessische Technische Hochschule Zuerich _/_/_/ _/ _/_/_/ Swiss Federal Institute of Technology Zurich _/ _/ _/ _/ Ecole polytechnique federale de Zurich _/_/_/ _/ _/ _/ Politecnico federale de Zurigo Make it as simple as possible, but distrust it! ________________________________________________________________________ |
[Prev in Thread] | Current Thread | [Next in Thread] |