# Function return array

**URL:** <https://verificationacademy.com/forums/t/function-return-array/34559>\
**Category:** UVM\
**Tags:** UVM, function-return-array\
**Created:** [June 11, 2018, 12:36pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559 "2018-06-11T12:36:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalithjithan](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@lalithjithan](https://verificationacademy.com/forums/u/lalithjithan)\
**Post date:** [June 11, 2018, 12:36pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559/1 "2018-06-11T12:36:09Z")

</div>

```verilog
function bit [7:0]payload_calc payl(int length,int data_size,int data[]);
    payl = new[length];
    reg [31:0][7:0] temp_data;//varible(packed array) used to to convert 256 bit data in to 8 bit form
    int a =0;
    reg [7:0] temp_payload[];
    temp_payload = new[length];
    for(int i=0;i<data_size;i++)begin
        temp_data = data[i];//data is 256 bit array
        for(int j=0;(j<32 && a<length);j++)begin
           temp_payload[a] = temp_data[j];
           `uvm_info("TEMP_PAYLOAD",$sformatf("temp_payload[%0d] : %0h",a,temp_payload[a]),UVM_HIGH)
           `uvm_info("TEMP_DATA",$sformatf("tmp[%0d] : %0h",j,temp_data[j]),UVM_HIGH)
           a++;
        end
    end
      payload_calc= temp_payload;
    endfunction : payload_calc

```

i want a function which return the array … but i trying the above one which shows compilation error…how to overcome this error

thanks

---

<div class="post-metadata">

**Author:** ![saravanan\_kpk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@saravanan\_kpk](https://verificationacademy.com/forums/u/saravanan_kpk)\
**Post date:** [June 12, 2018, 4:10am UTC](https://verificationacademy.com/forums/t/function-return-array/34559/2 "2018-06-12T04:10:05Z")

</div>

_In reply to [lalithjithan](https://verificationacademy.com/forums/t/function-return-array/34559):_

Follow the example given in stackoverflow

> <https://stackoverflow.com/questions/13963935/returning-queue-from-function-in-systemverilog>

---

<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:** [June 12, 2018, 4:20am UTC](https://verificationacademy.com/forums/t/function-return-array/34559/3 "2018-06-12T04:20:16Z")

</div>

_In reply to [lalithjithan](https://verificationacademy.com/forums/t/function-return-array/34559/2):_

The syntax for function declarations does permit anything other than simple types and type names. So you must use a typedef name fore the return type.

```verilog
typedef bit [7:0] byte_da_t[];
function byte_da_t payl(int length,int data_size,int data[]);

```

---

<div class="post-metadata">

**Author:** ![aniltirumalasetty](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@aniltirumalasetty](https://verificationacademy.com/forums/u/aniltirumalasetty)\
**Post date:** [April 16, 2019, 12:59pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559/4 "2019-04-16T12:59:13Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/function-return-array/34559/3):_

mr dave,

can you give an SV example to usege of function as an array?

regards,  
Anil T

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [April 17, 2019, 3:01pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559/5 "2019-04-17T15:01:48Z")

</div>

_In reply to [anil tirumalasetty](https://verificationacademy.com/forums/t/function-return-array/34559/4):_

Try this code please:

```verilog
module top;
  import uvm_pkg::*;
  `include "uvm_macros.svh"

  
  typedef bit [7:0] byte_da_t[];
         
  function byte_da_t payl(int length,int data_size,int data[]);
    byte_da_t temp_payload;
    
    logic [31:0][7:0] temp_data;//varible(packed array) used to to convert 256 bit data in to 8 bit form
    int a;
    payl = new[length];
    a =0;
   
    temp_payload = new[length];
    for(int i=0;i<data_size;i++)begin
        temp_data = data[i];//data is 256 bit array
        for(int j=0;(j<32 && a<length);j++)begin
           temp_payload[a] = temp_data[j];
           `uvm_info("TEMP_PAYLOAD",$sformatf("temp_payload[%0d] : %0h",a,temp_payload[a]),UVM_HIGH)
           `uvm_info("TEMP_DATA",$sformatf("tmp[%0d] : %0h",j,temp_data[j]),UVM_HIGH)
           a++;
        end
    end
      return temp_payload;
  endfunction : payload_calc
  
  initial begin
    
  end  
  
endmodule

```

---

<div class="post-metadata">

**Author:** ![Athiras](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e274bd/32.png) [@Athiras](https://verificationacademy.com/forums/u/Athiras)\
**Post date:** [May 7, 2019, 2:54pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559/6 "2019-05-07T14:54:03Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/function-return-array/34559/5):_

Mr chr\_sue

When we use the _typedef bit [7:0] byte\_da\_t[];_,  
should we use the same type while calling the function ?

Br,  
Athira

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [May 7, 2019, 4:49pm UTC](https://verificationacademy.com/forums/t/function-return-array/34559/7 "2019-05-07T16:49:32Z")

</div>

_In reply to [Athiras](https://verificationacademy.com/forums/t/function-return-array/34559/6):_

Of course, because your function is of this type.
