RE: Passing a function to another function

From: Peter Ashenden <peter_at_.....>
Date: Tue Feb 05 2008 - 22:19:46 PST
Mani,

Thanks for your question. I have cc'ed to the vhdl-ams list, as the topic is
relevant to possible future development of the language.

As VHDL-AMS currently stands, there is no mechanism to do what you want. In
the VHDL-2008 revision of VHDL, however, features have been added that can
be used to pass function, in a sense. A function can include generics that
are also functions, not just constants.

To illustrate how that might work, here's a function that integrates some
function f from a to b using the trapezoid rule with n subintervals:

  function integrate
    generic ( function f(x:real) return real )
    (a, b : real; n : positive) return real is;
  begin
    sum := (f(a) + f(b))/2;
    for k in 1 to n-1 loop
      sum := sum + f(a + k*(b - a)/n);
    end loop;
    return sum * (b - a)/n;
  end function integrate;

To apply this to some particular function, we need to instantiate it and
provide the integrand function as the actual generic for f. For example,
given a sqr function:

  function sqr(x:real) return real is
  begin
    return x*x;
  end function sqr;

We could instantiate the integration function as

  function integrate_sqr is new integrate
    generic map ( f => sqr );

We could then call the instance as follows:

  integ := integrate_sqr(0.0, 5.0, 100);

Note that the language features described here is not currently in VHDL-AMS,
as VHDL-AMS is based on the 2002 revision of VHDL. If VHDL-AMS gets revised
to extend VHDL-2008, the features would become part of the language
standard. They would then need to be incorporated into tools to become
useful. Might be a bit pie-in-the-sky at this stage.

Cheers,

PA

--
Dr. Peter J. Ashenden         peter@ashenden.com.au
Ashenden Designs Pty. Ltd.    www.ashenden.com.au
PO Box 640                    VoIP: sip://0871270078@sip.internode.on.net
Stirling, SA 5152             Phone: +61 8 7127 0078
Australia                     Mobile: +61 414 70 9106
 

> -----Original Message-----
> From: Sivaramakrishnan, Subramanian 
> [mailto:subramanian.sivaramakrishnan@hp.com] 
> Sent: Wednesday, 6 February 2008 10:24
> To: Peter Ashenden
> Subject: Passing a function to another function
> 
> 
> Hello:
> 
> I have a question on how to implement a function that needs 
> to call another function which is not yet known. An example 
> will be a function to implement integration of an arbitrary 
> function (integrand) but the integrand function is not known 
> while writing the code for the integration function. What is 
> the mechanism to pass the integrand function to the 
> integration function. Can the integrand function be passed 
> like a parameter?
> 
> I was not sure if this question will be of sufficient 
> interest to the general vhdl-ams community and hence I am 
> addressing it to you. You may choose to CC your solution to 
> the general community if you wish.
> 
> 
> Thanks for your help.
> 
> Mani
> HP, Corvallis, OR.
> 


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Tue Feb 5 22:20:19 2008

This archive was generated by hypermail 2.1.8 : Tue Feb 05 2008 - 22:20:55 PST