Can a function return unpacked arrays like queue/Dynamic arrays?

Hello All,

How do we return an queue or dynamic array from a function? I’ve tried and getting compilation error.

Is it possible?


Yes you can, but you must use a typedef to define the return type. That is because the BNF does not allow you to put an array range to the right of a function name.

typedef bit bitstream_t[$];
function bitstream_t my_funct(input bitstream_t arg);

Dave Rich

In reply to dave_59:

I did this but an error says “inappropriate use of bit select” pointing to chk_data_error[i].
This is my code:

typedef bit data_error[];

virtual protected function data_error chk_data_error (int unsigned bc, bit [8:0] data_bit_err[]);
  this.chk_data_error = new[bc];
  for(int i=0; i<bc; i++) begin
    this.chk_data_error[i] = |data_bit_err[i];
endfunction : chk_data_error

In reply to Reuben:

Don’t put
in front of the function when using it as a return value.

virtual function data_error chk_data_error (bit [8:0] data_bit_err[]);
  chk_data_error = new[data_bit_err.size];
    chk_data_error[i] = |data_bit_err[i];
endfunction : chk_data_error

In reply to dave_59:

Ok thanks! It’s working! =)