systemverilog return dynamic array from function

What do you mean by "fully dynamic" an array is either fixed, or dynamically sized. There is no in-between.

Commented Mar 28, 2017 at 12:14 Perhaps you want a queue or associative array? Commented Mar 28, 2017 at 15:48

2 Answers 2

You can pass the dynamic array by reference in the function for your purpose.

Here is the sample code for it.

module tp(); integer a[]; initial begin return_x(a); $display("a - %p", a); end endmodule function automatic void return_x(ref integer x[]); x = new [3]; x = '; endfunction // Output - // a - '
answered Mar 29, 2017 at 6:15 Karan Shah Karan Shah 1,972 1 1 gold badge 30 30 silver badges 46 46 bronze badges

If you want to return the dynamic array using return in your function, then you need a typedef.

Typedef is needed when you want a function to return an unpacked type.

typedef int registerdynamic_t[]; function automatic registerdynamic_t return_dyn_arr get_register_name(int data_len=2); return_dyn_arr = new [data_len] ; //you can use a for loop to put values in your dynamic array return_dyn_arr [0] = 5; return_dyn_arr [1] = 2; return return_dyn_arr ; endfunction