9

Is it possible to pass a function as an argument in SystemVerilog?

This code hopefully demonstrates though it doesn't work. Any help? Thanks.

    module funcparam;
            int result;

            function int xxx(int x, ref fun);
                    return fun(x);
            endfunction

            function int yyy(int y);
                    return y * (y + y);
            endfunction

            initial begin
                    result = xxx(5, yyy);
                    $display("result: %d", result);
            end
    endmodule
e19293001
  • 2,783
  • 9
  • 42
  • 54

2 Answers2

6

You're limited as to what can be passed by reference:

  • a variable,
  • a class property,
  • a member of an unpacked structure, or
  • an element of an unpacked array.

You might be able to pass in a handle of a base class, though I doubt this would work.

class base;
  function yyy(int x);
  endfunction
endclass

class class1 extends base;
  function yyy(int x);
  endfunction
endclass

class class2 extends base;
  function yyy(int x);
  endfunction
endclass


module funcparam;
   result;

   function int xxx(int x,input base fun);
     return fun.yyy(x);
   endfunction

   class1 cls = new;
   //class2 cls = new;


    initial begin
       result = xxx(5, cls);
      $display("result: %d", result);
    end
endmodule
4

No.

Tasks and functions can only accept data types as arguments, and functions are not data types. Also there is no way to make a function into a data type.

dwikle
  • 6,820
  • 1
  • 28
  • 38