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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4b807da453e88972 X-Google-Attributes: gid103376,public From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk) Subject: Re: Function Pointers Date: 1998/04/14 Message-ID: #1/1 X-Deja-AN: 344038115 References: <353257CC.211B84EE@phaseiv.com> Organization: * JerryWare *, Leiden, Holland Newsgroups: comp.lang.ada Date: 1998-04-14T00:00:00+00:00 List-Id: 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