Pass array of unknown size to function

I have the following function:

   function void foo_arr_bit (int seed, ref bit [*] mem, string  mem_name);
      for (int i=0; i< mem.size(); i++)
    mem[i] = my_randomize_int(seed, mem[i], mem_name);     
   endfunction: foo_arr_bit

I call the function by:

 foo_arr_bit(seed, data_bit, "data_bit");

Where data_bit can be: bit [1:0]/ bit [2:0]/ bit [3:0]/ bit [4:0]/ bit [5:0] etc…

When I try to compile I got the following error: near “[”: syntax error, unexpected [ , expecting IDENTIFIER or TYPE_IDENTIFIER or NETTYPE_IDENTIFIER.

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.

In reply to dave_59:

I tried to call the foo function with bit vector with size of 8, and got the following error:
Illegal function inout argument.

Foo function:

function void foo_arr_bit (inout bit [31:0] mem, 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

Calling foo:

foo_arr_bit (data_bit, 8, "data_bit");

In reply to dave_59:

After too many time of debug:( I founnd out that the error is because I didn’t write input before int size (in the arguments). Why shold I write this? input should be the default port…

In reply to saritr:
Please re-read the last paragraph of my response.

In reply to dave_59:

Thanks, I missed the two last lines.

In reply to dave_59:

Just one more question
Can I do the foloowing:

int array_int [$];

foo_array_int(seed, array_int, "array_int");

Where foo_array_int is:

 function void foo_array_int (int seed, ref int q [$], input string mem_name);
      int temp;      
      for (int i = 0; i < q.size(); i++) begin
	 q.push_back(foo_int(seed, temp, mem_name));
      end	
   endfunction: foo_array_int

p.s
When I debug q.size() is allways 0…

In reply to saritr:
You need to initialize array_int before calling foo_array_int(), or you need to do something else inside foo_array_int