From: Ingo Marks <adv@region-nord.de>
Subject: Re: Out parameters in a function
Date: Wed, 17 Apr 2002 13:55:18 +0200
Date: 2002-04-17T13:55:18+02:00 [thread overview]
Message-ID: <a9jnq7$uaj$04$1@news.t-online.com> (raw)
In-Reply-To: a9chqr$23e76$1@ID-107015.news.dfncis.de
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
prev parent reply other threads:[~2002-04-17 11:55 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-14 18:29 Out parameters in a function Nazgul
2002-04-14 19:45 ` David C. Hoos, Sr.
2002-04-15 10:49 ` John McCabe
2002-04-15 11:51 ` John McCabe
2002-04-15 13:43 ` Steve Doiel
2002-04-15 15:09 ` Ted Dennison
2002-04-16 8:49 ` John McCabe
2002-04-16 10:05 ` Dmitry A. Kazakov
2002-04-16 15:26 ` John McCabe
2002-04-16 19:34 ` Matthew Woodcraft
2002-04-16 20:10 ` Darren New
2002-04-17 1:30 ` Kent Paul Dolan
2002-04-17 16:15 ` Darren New
2002-04-19 17:39 ` Florian Weimer
2002-04-17 13:24 ` Stephen Leake
2002-04-17 16:32 ` Darren New
2002-04-17 21:03 ` Kent Paul Dolan
2002-04-18 12:14 ` Wolfgang Gellerich
2002-04-18 13:52 ` Dmitry A. Kazakov
2002-04-18 16:28 ` Darren New
2002-04-17 10:17 ` John McCabe
2002-04-16 19:58 ` Kent Paul Dolan
2002-04-16 20:00 ` Kent Paul Dolan
2002-04-17 4:40 ` Jim Rogers
2002-04-17 5:27 ` Kent Paul Dolan
2002-04-17 5:50 ` Eric G. Miller
2002-04-24 2:45 ` David Thompson
2002-04-19 17:40 ` Florian Weimer
2002-04-19 18:26 ` Jim Rogers
2002-04-19 18:53 ` Florian Weimer
2002-04-17 7:57 ` Dmitry A. Kazakov
2002-04-17 10:21 ` John McCabe
2002-04-24 17:21 ` Warren W. Gay VE3WWG
2002-04-26 7:32 ` Dmitry A. Kazakov
2002-04-15 16:24 ` Stephen Leake
2002-04-16 13:38 ` Ted Dennison
2002-04-17 11:55 ` Ingo Marks [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox