* Function Pointers
@ 1998-04-13 0:00 Barry L. Dorough
1998-04-13 0:00 ` Matthew Heaney
1998-04-14 0:00 ` Jerry van Dijk
0 siblings, 2 replies; 5+ messages in thread
From: Barry L. Dorough @ 1998-04-13 0:00 UTC (permalink / raw)
Is there any way to implement function pointers in Ada? Below is a
sample C program using function pointers. How can I do this in Ada?
Thanks
in advance,
Barry
Dorough
#include <iostream.h>
double Add( double x, double y );
double Subtract( double x, double y );
double Combine( double p, double q, double (*fcn)(double r, double s) );
int main()
{
double w = 20.0;
double x = 5.0;
double y = Combine( w, x, Add );
double z = Combine( w, x, Subtract );
cout << w << " " << x << " " << y << " " << z << endl;
return 0;
}
double Add( double x, double y )
{
return x+y;
}
double Subtract( double x, double y )
{
return x-y;
}
double Combine( double p, double q, double (*fcn)(double r, double s) )
{
double a = 10.0 + p;
return fcn( a, q );
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function Pointers
1998-04-13 0:00 Function Pointers Barry L. Dorough
@ 1998-04-13 0:00 ` Matthew Heaney
1998-04-14 0:00 ` Jerry van Dijk
1 sibling, 0 replies; 5+ messages in thread
From: Matthew Heaney @ 1998-04-13 0:00 UTC (permalink / raw)
In article <353257CC.211B84EE@phaseiv.com>, "Barry L. Dorough"
<bdorough@phaseiv.com> wrote:
>Is there any way to implement function pointers in Ada? Below is a
>sample C program using function pointers. How can I do this in Ada?
[snip]
>double Combine( double p, double q, double (*fcn)(double r, double s) )
>{
> double a = 10.0 + p;
> return fcn( a, q );
>}
Declare a named access type, as follows:
package P is
type Func_Access is access function (R, S : Long_Float) return Long_Float;
function Combine (P, Q : Long_Float; Func : Func_Access) return Long_Float;
end P;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function Pointers
1998-04-13 0:00 Function Pointers Barry L. Dorough
1998-04-13 0:00 ` Matthew Heaney
@ 1998-04-14 0:00 ` Jerry van Dijk
1998-04-15 0:00 ` Robert Dewar
1 sibling, 1 reply; 5+ messages in thread
From: Jerry van Dijk @ 1998-04-14 0:00 UTC (permalink / raw)
Barry L. Dorough (bdorough@phaseiv.com) wrote:
: Is there any way to implement function pointers in Ada?
Yes, basically the same way as in C. However, being a stronger
typed language, Ada asks you to explicitly create a pointer type
for the functions you want to access through a pointer, so that the
compiler can check argument and return types.
: Below is a
: sample C program using function pointers. How can I do this in Ada?
Actually, your program is C++, not C. Anyway:
-- As GNAT allows only one compilation unit in a file
-- we nest the functions within main;
-- Long_Float would be GNAT's equivalent to gcc's double;
-- We pretend that Main is a 'void' function.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Func_Ptr is access function (X, Y : Integer) return Integer;
-- Declare a pointer type, pointing to a function that takes
-- two Integer variables as input and returns a Integer
function Add (X, Y : Integer) return Integer is
begin
return X + Y;
end Add;
function Subtract (X, Y : Integer) return Integer is
begin
return X - Y;
end Subtract;
function Combine (P, Q : Integer; Func : Func_Ptr) return Integer is
A : Integer := 10 + P;
begin
return Func (A, Q);
end Combine;
W : Integer := 20;
X : Integer := 5;
Y : Integer := Combine (W, X, Add'Access);
Z : Integer := Combine (W, X, Subtract'Access);
begin
Put_Line (W'Img & " " & X'Img & " " & Y'Img & " " & Z'Img);
end Main;
--
-- Jerry van Dijk | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function Pointers
1998-04-14 0:00 ` Jerry van Dijk
@ 1998-04-15 0:00 ` Robert Dewar
1998-04-16 0:00 ` Jerry van Dijk
0 siblings, 1 reply; 5+ messages in thread
From: Robert Dewar @ 1998-04-15 0:00 UTC (permalink / raw)
Jerry said
<<-- As GNAT allows only one compilation unit in a file
-- we nest the functions within main;
>>
Surely a bad reason for doing anything. It is bad practice (see for example
AQ&S) generally to put more than one compilation unit in a file anyway.
But the issue of whether to write a single compilation unit or many
compilation units is surely a design issue that should not be affected
by low level representation issues (i.e. how compilation units are
represented in files, whether you want to use gnatchop if you are using
GNAT etc.)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Function Pointers
1998-04-15 0:00 ` Robert Dewar
@ 1998-04-16 0:00 ` Jerry van Dijk
0 siblings, 0 replies; 5+ messages in thread
From: Jerry van Dijk @ 1998-04-16 0:00 UTC (permalink / raw)
Robert Dewar (dewar@merv.cs.nyu.edu) wrote:
: Jerry said
: <<-- As GNAT allows only one compilation unit in a file
: -- we nest the functions within main;
: >>
: Surely a bad reason for doing anything. It is bad practice (see for example
: AQ&S) generally to put more than one compilation unit in a file anyway.
Agreed. I was just being lazy.
--
-- Jerry van Dijk | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1998-04-16 0:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-04-13 0:00 Function Pointers Barry L. Dorough
1998-04-13 0:00 ` Matthew Heaney
1998-04-14 0:00 ` Jerry van Dijk
1998-04-15 0:00 ` Robert Dewar
1998-04-16 0:00 ` Jerry van Dijk
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox