SourceSV Verification Directory

Functions

  1. A function must return a value.
  2. A function should not include delays, event controls, or wait statements.
  3. A function can only contain inputs as arguments as it returns only one output value.

Similar to tasks:

  1. A function can be declared as automatic or static.
  2. It can contain static variables in automatic function and vice-versa.

Syntax: Function

function [return type] function_name  (input_arguments);
   // execute the code
   return [value];
endfunction

Example Code: Function

module tb_function_ex;

  function int mul_3(int x);
    return x*3;
  endfunction

  initial begin
    int x;
    $display("---------- Multiply by 3 ----------");
    repeat(20)begin
      x = $urandom_range(1,9);
      $display("x = %0d, mul_by_3 = %0d", x, mul_3(x));
    end
  end

endmodule

Execute the code in EDA Playground