Article View: comp.lang.modula2
Article #1485Re: Oberon parameter passing
From: Phil Scadden
Date: Sun, 04 Oct 1992 20:47
Date: Sun, 04 Oct 1992 20:47
44 lines
2482 bytes
2482 bytes
> I have a question about passing of array parameters in Oberon-2.
> Lets say I want to write a procedure to print a string passed in to
> it. I would do something like:
>
> PROCEDURE PrintString(VAR s : ARRAY OF CHAR);
> BEGIN
> (* print string here *)
> END Print String.
>
> But then the following would not work.
>
> PrintString("Hello");
>
> Because a constant is being passed in. So instead, the PrintString
> routine would be:
>
> PROCEDURE PrintString(s : ARRAY OF CHAR);
> BEGIN
> (* print string here *)
> END Print String.
Well here is one my pet weaknesses with PASCAL/M2/Oberon??. In parameter
passing the VAR (or its absence) is being forced into a dual service
of logically (from the programmers point of view) indicating whether a
parameter will be modified or not by the routine; and from the compiler
point of view, it is used for determining passing mechanism. Strictly
the programmer shouldnt have to worry about such implementation-dependent
detail but in practise the need for efficiency comes in. Many "real"
compilers use "pragmas" or "attributes" to convey that kind of extra
information to the compiler and personally I would like to see this practise
made consistant in the language. Ie allow optional attributes like
[READONLY] on parameters.
Returning to your example, I dont see a problem with
PROCEDURE PrintString(VAR s : ARRAY OF CHAR);
BEGIN
(* print string here *)
END PrintString.
Your compiler creates "hello" in a write-protected page (if your memory
hardware will support such a beast). It has an address like any other variable
which can be passed. If your routine tries to modify it, then a memory
exception will be generated at runtime. A lot of smarts needed to flag such
a problem at compile time though without the pragma/attribute approach.
Phil
Technical Details
Message-ID:
<1992Oct5.084755.8205@lhn.gns.cri.nz>Path:
arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!usc!wupost!waikato.ac.nz!comp.vuw.ac.nz!windy!lhn!mdlcpgsReferences:
<1992Oct01.203420.3612@bnr.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!usc!wupost!waikato.ac.nz!comp.vuw.ac.nz!windy!lhn!mdlcpgs Newsgroups: comp.lang.modula2 Subject: Re: Oberon parameter passing Message-ID: <1992Oct5.084755.8205@lhn.gns.cri.nz> From: mdlcpgs@lhn.gns.cri.nz (Phil Scadden) Date: 5 Oct 92 08:47:55 +1200 References: <1992Oct01.203420.3612@bnr.ca> Organization: Institute of Geological and Nuclear Sciences, Lower Hutt, New Zealand Lines: 44