Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Functions & Tasks: const ref: Reference Efficiency Without the Mutability Risk

SystemVerilog Advanced Features

Runnable example from the SystemVerilog Functions & Tasks tutorial (const ref: Reference Efficiency Without the Mutability Risk)

Open in IDE
testbench.sv
function automatic int sum_it (const ref int a []);
  int total = 0;

  foreach (a[i])
    total += a[i];

  // a[0] = 99;   // Illegal: 'a' is const, this is a compile-time error

  return total;
endfunction

module tb;
  initial begin
    int data[] = '{1, 2, 3, 4, 5};
    $display ("sum = %0d", sum_it(data));
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.