# Pass array to a function

**URL:** <https://verificationacademy.com/forums/t/pass-array-to-a-function/35469>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-Arrays\
**Created:** [December 14, 2018, 3:43pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469 "2018-12-14T15:43:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![verif\_learner1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@verif\_learner1](https://verificationacademy.com/forums/u/verif_learner1)\
**Post date:** [December 14, 2018, 3:43pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/1 "2018-12-14T15:43:41Z")

</div>

I am passing an array to a function. I don’t know the size of the array that a caller will use.

Here is what I am observing:  
case 1) when I use like below, I see an error “Illegal ref port connection”

```verilog
function func1 (ref int a []);

```

case 2) when I use this below, where I remove the ref, I don’t see an error

```verilog
function func1 (int a []);

```

case 3) when I use like below, where I specify the size of the array, I don’t see an error

```verilog
function func1 (ref int a [5]);

```

One full example is shown below:

```verilog
module x;
  function func1 (ref int a [5]);
    $display ("%p", a);
  endfunction
  
  initial begin
    int a [5] = {1,2,3,4,5};
    func1(a);
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 14, 2018, 4:27pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/2 "2018-12-14T16:27:57Z")

</div>

_In reply to [verif\_learner](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469):_

There are many more restrictions when using pass by reference direction. The formal and actual argument types must match exactly. A fixed sized array is not the same type as a dynamic array. Another restriction your tool did not catch is you can only use pass by reference with functions/tasks that have an automatic lifetime. [See this link](https://verificationacademy.com/forums/systemverilog/what-meant-reference-argument-must-be-automatic#reply-41381).

Also, you are declaring your function without an explicit return type. The Verilog implicit default is 1-bit return value. Use ‘function void’

---

<div class="post-metadata">

**Author:** ![verif\_learner1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@verif\_learner1](https://verificationacademy.com/forums/u/verif_learner1)\
**Post date:** [December 14, 2018, 5:33pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/3 "2018-12-14T17:33:02Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/2):_

> _In reply to [verif\_learner](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/2):_  
> There are many more restrictions when using pass by reference direction. The formal and actual argument types must match exactly. A fixed sized array is not the same type as a dynamic array. Another restriction your tool did not catch is you can only use pass by reference with functions/tasks that have an automatic lifetime. [See this link](https://verificationacademy.com/forums/systemverilog/what-meant-reference-argument-must-be-automatic#reply-41381).  
> Also, you are declaring your function without an explicit return type. The Verilog implicit default is 1-bit return value. Use ‘function void’

ok. So, what is the option available if I want to pass an array as an argument to a function if I do not know the size of the array. I assume this is a very common issue in verification.  
For example, if I am passing a array that contains packet data to the function, most likely I will not know the size of the packet at all.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 14, 2018, 7:02pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/4 "2018-12-14T19:02:04Z")

</div>

_In reply to [verif\_learner](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/3):_

Usually, the packet is a class object with an arrays as a member. You just pass the object’s handle as an input to the function.

If you make sure you always use a dynamic array, you can pass an array by reference.

```verilog
module x;
  function func1 (ref int a []);
    $display ("%p", a);
  endfunction
 
  initial begin
    int a [];
    a = {1,2,3,4,5};
    func1(a);
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![verif\_learner1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@verif\_learner1](https://verificationacademy.com/forums/u/verif_learner1)\
**Post date:** [December 15, 2018, 4:21pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/5 "2018-12-15T16:21:29Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/4):_

> _In reply to [verif\_learner](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/4):_  
> Usually, the packet is a class object with an arrays as a member. You just pass the object’s handle as an input to the function.  
> If you make sure you always use a dynamic array, you can pass an array by reference.

```verilog
module x;
function func1 (ref int a [5]);
$display ("%p", a);
endfunction
initial begin
int a [];
a = {1,2,3,4,5};
func1(a);
end
endmodule

```

Dave,

I am going through LRM section 7.7.  
This is what it says,

> A subroutine that accepts a dynamic array or queue can be passed a dynamic array, queue, or fixed-size array of a compatible type.  
> For example, the declaration  
> task t ( string arr );  
> declares a task that accepts one argument, a dynamic array of strings. This task can accept any onedimensional unpacked array of strings or any one-dimensional dynamic array or queue of strings.

I am interpreting this as if the formal argument to the function is dynamic array then one can call this function with another dynamic array.  
Am I interpreting something incorrectly?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 15, 2018, 4:46pm UTC](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/6 "2018-12-15T16:46:11Z")

</div>

_In reply to [verif\_learner](https://verificationacademy.com/forums/t/pass-array-to-a-function/35469/5):_

Yes, I made a mistake in my example. Now they are both dynamic arrays. BTW, section 7.7 discusses the rules for passing arrays by value. Passing by references has more strict rules.
