From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9245b8db9abd376c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-04-17 04:55:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.com!t-online.de!news.t-online.com!not-for-mail From: Ingo Marks Newsgroups: comp.lang.ada Subject: Re: Out parameters in a function Date: Wed, 17 Apr 2002 13:55:18 +0200 Organization: T-Online Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1019044488 04 31059 7dS9TCPXSYYk0N 020417 11:54:48 X-Complaints-To: abuse@t-online.com X-Sender: 340020534592-0001@t-dialin.net User-Agent: KNode/0.6.1 Xref: archiver1.google.com comp.lang.ada:22646 Date: 2002-04-17T13:55:18+02:00 List-Id: Nazgul wrote: > Hi, I need to use an output parameter in an Ada function, something like > > function ReadChar(f: File; c: out character) return boolean; > > The function must return a boolean, so the only way to read the character > is via the 'c' parameter. In other context, I use > > function (something: tpSomething) return ...; where tpSomething is an > access to a record type, and I can modify the contents of the record in > the function, but if I use > > function ReadChar(f: File; c: access character) return boolean; > > the compiler gives an error when i do something like > > c:=anything; > > Is there any way of using c as an output parameter? > > Thanks. You can simulate out parameters in functions with access: procedure Test is function Increment (Var : access Integer; Max : Integer) return Boolean is Value : Integer renames Var.all; begin Value := Value + 1; return Value <= Max; end; X : aliased Integer := 0; begin for I in 1..5 loop if Increment (X'Access, 3) then Put_Line("ok"); else Put_Line("overflow"); end if; end loop; end Test; Result: ok ok ok overflow overflow Sometimes it may be better to use exceptions in favor of boolean tests: procedure Test is procedure Increment (Value : in out Integer; Max : Integer) is begin Value := Value + 1; if Value > Max then raise Constraint_Error; end if; end; X : Integer := 0; begin for I in 1..5 loop begin Increment (X, 3); Put_Line("ok"); exception when Constraint_Error => Put_Line("overflow"); end; end loop; end Test; If you define your own integer type with range 1..3 then you can spare the conditional statement in the function. The program will raise a runtime constraint error automatically provided that you have used the necessary compiler options. Regards, Ingo