6

I'm reading some third party Verilog, and found this:

function [31:0] factorial;
    input [3:0] operand;
    reg [3:0] index;

    begin
        factorial = operand ? 1 : 0;
        for(index = 2; index <= operand; index = index + 1)
        factorial = index * factorial;
    end
endfunction

It seems that the begin and end keywords are redundant here. Are they? What is their use?

toolic
  • 57,801
  • 17
  • 75
  • 117
Randomblue
  • 112,777
  • 145
  • 353
  • 547

3 Answers3

10

I don't know about the general case, but in this specific case:

If a function contains more than one statement, the statements must be enclosed in a begin-end or fork-join block.

Source: Verilog Golden Reference Guide

Tim
  • 35,413
  • 11
  • 95
  • 121
  • 1
    `begin/end` is no longer required for a `function` or `task` with multiple statements in SystemVerilog. See other answers. – dave_59 Feb 24 '15 at 00:01
7

Both answers are correct. If the Verilog task or function had multiple statements, they were also required to have begin-end statements. Starting in SystemVerilog-2005, we removed the requirement to put begin-end inside of something that already had a begin-end. Most of us on the committee thought it was silly to require a begin-end inside of something that was already going to have endfunction/endtask. I mean, come-on! Don't you think a compiler could figure out that when it got the endtask/endfunction statement that it was at the end of the task or function?? Removing the begin-end from tasks and functions cuts out a surprising amount of useless code. Score another point for SystemVerilog!

Cliff Cummings
  • 929
  • 1
  • 7
  • 9
1

According to the SystemVerilog extension (IEEE Standard 1800-2009), begin/end are optional inside a function. However, your toolset (simulator, etc.) must be capable of understanding this syntax, which was introduced in 2005.

toolic
  • 57,801
  • 17
  • 75
  • 117