Pass array of unknown size to function

In reply to saritr:
You are not clear if you want an unpacked array or packed array. An argument called “mem” usually refers to a memory, which is an unpacked array. And the code in your for loop seems to imply that you think each mem[i] is an int.

But I will assume mem is a packed array. If that is the case, what you can do declare your mem argument using the maximum size you expect data_bit to be, and use an inout argument instead of a ref argument. Using an inout lets you pass an argument of a different size to mem, whereas a ref argument requires an exact matching type. You will also need to pass the size of data_bit.

function void foo_arr_bit (inout bit [31:0] mem, input int size, string  mem_name);
      for (int i=0; i< size(); i++)
           mem[i] = my_randomize_int(mem[i], mem_name);     
endfunction: foo_arr_bit

Another thing to point out is that whenever you put the qualifiers"ref, input, output, or inout in an argument list, that qualifier remains implicit in the arguments that follow until there is another explicit qualifier. So in your original example, mem_name is implicitly a ref argument.