Prev Problem
Next Problem

57. Using Functions

module parity_and_reverse (
    input  [7:0] a,
    output       parity,
    output [7:0] rev
);
    function parity8;
        input [7:0] x;
        begin
            parity8 = ^x;
        end
    endfunction

    function [7:0] reverse8;
        input [7:0] x;
        integer i;
        begin
            for (i = 0; i < 8; i = i + 1)
                reverse8[i] = x[7-i];
        end
    endfunction

    assign parity = parity8(a);
    assign rev    = reverse8(a);
endmodule

💡Remember

  • Functions are pure combinational helpers: one return value, no timing controls.
  • Great for reuse/clarity—call them in continuous assigns or inside always @*.
  • Keep outputs fully driven in combinational logic to avoid latches.