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.2 required=5.0 tests=BAYES_00,FROM_LOCAL_HEX, FROM_STARTS_WITH_NUMS autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,de7dd126d6737f3a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Vinzent Hoefler" <0439279208b62c95f1880bf0f8776eeb@t-domaingrabbing.de> Newsgroups: comp.lang.ada Subject: Re: Callback in Ada Date: Sat, 27 Nov 2010 11:46:40 +0100 Message-ID: References: <8lc2d0Fb6jU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Trace: individual.net D3b+JsaBa6SGHoAyIkwkBQ0oC++IUMro518T36Gem2EMgTS/wf Cancel-Lock: sha1:bNS3Vfl6tf1PoSRBFNhoWoJ3nXY= User-Agent: Opera Mail/10.63 (Win32) Xref: g2news2.google.com comp.lang.ada:16647 Date: 2010-11-27T11:46:40+01:00 List-Id: Georg Maubach wrote: > today I learnt what a callback function is and also how it is used in > Python. I have tested the following code which runs in Python: > > def apply_to (function, valueList): [...] > Values doubled: 2, 4, 6, 8, 10 > Values powered: 1, 4, 9, 16, 25 > > How would I do that in Ada? The important thing is the Callback type and the 'Access attribute for the function which you like to call. -- 8< -- with Ada.Text_IO; procedure Callback is function Duplicate (Value : in Integer) return Integer is begin return Value * 2; end Duplicate; function Power (Value : in Integer) return Integer is begin return Value * Value; end Power; type Int_List is array (Positive range <>) of Integer; type Callback is access function (Value : in Integer) return Integer; procedure Apply_To (Operation : Callback; Values : Int_List) is R : Integer; begin for V in Values'Range loop R := Operation (Values (V)); Ada.Text_IO.Put (Integer'Image (R)); end loop; Ada.Text_IO.New_Line; end Apply_To; Value_List : constant Int_List := (1, 2, 3, 4, 5); begin Apply_To (Duplicate'Access, Value_List); Apply_To (Power'Access, Value_List); end Callback; -- 8< -- Vinzent.