Search code examples
functionparameterscompiler-errorsvhdl

VHDL function with no parameters?


Is it possible to define a VHDL function with no parameters?

I'm trying to compute the number of bits in a record type, but to do so it is necessary to create an instance of that type. Therefore, since I can't just define const BITS = t_rec.a'length + t_rec.b'length, I would like to define a function function BITS() return natural that instantiates a record rec : t_rec and returns rec.a'length + rec.b'length. However, the compiler fails with unexpected '(') at the function declaration, before it even reaches the definition.

I would just include an unused, dummy parameter, but I suspect that the lint tools would complain.

Defining subtypes for each record field in advance of the record would be too verbose.

Complete example:

package pack is function BITS() return natural; end package pack; package body pack is function BITS() return natural is begin return 0; end function; end package body pack;

Error message:

pack.vhd:1:30: empty interface list not allowed


Solution

  • You should leave out the parentheses when defining a function with no inputs, eg:

    function BITS return natural is
      variable rec : rec_t;
    begin
      return rec.a'length + rec.b'length;
    end function;
    

    You should not include the parentheses when you call the function, either. eg:

    report integer'image( BITS );
    

    not

    report integer'image( BITS() );
    

    https://www.edaplayground.com/x/5fMB