2

I am trying to return dynamic array from function, i did dome job but i dont know how to do that fully dynamic, which mean without declare the "data_len" thanks for helping,

module test1();

typedef integer dyn_arr[];

function dyn_arr get_register_name();
    int data_len = 3;
    get_register_name = new [data_len] ;
    get_register_name[0] = 5;
    get_register_name[1] = 2;

endfunction

 dyn_arr my_q;

  initial begin
       my_q = get_register_name();
      $display("%d",my_q[1]);
      $display("%d",my_q[0]);
  end

endmodule
3
  • Did you mean without variable declaration inside function? you can pass the size of dynamic array as argument to the function check this Commented Mar 28, 2017 at 11:22
  • 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

3

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 = '{3,3,3};
endfunction

// Output - 
// a - '{3, 3, 3}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

e.g.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.