Lesson 08: Tasks, Functions, and Directives

Tasks are like procedures in other programming languages, e.g., tasks may have zero or more arguments and do not return a value. Functions act like function subprograms in other languages. Tasks and functions serve different purposes in Verilog. We discuss tasks and functions in greater detail in the following sections. However, it is important to understand the differences between tasks and functions, as outlined in Table 10-1.

Table 8-1:Tasks and Functions

FunctionsTasks
A function can invoke (call, enable) another function, but not another task. A task can call other tasks and functions.
Functions always execute in 0 simulation time. Tasks may execute in non-zero simulation time.
Delay control (#), event control(@), and wait statements are prohibited in functions. Tasks may contain delay control (#), event (@), or timing control statements.
Functions must have at least one input argument. They can have more than one input. Tasks may have zero or more arguments of type input, output, or inout.
Functions always return a single value; the function name is the return variable. They cannot have output or inout arguments. Tasks do not return with a value but can pass multiple values through output and inout arguments.
Functions are used when common Verilog code is purely combinational, executes in zero simulation time, and provides exactly one output. Tasks are used for common Verilog code that contains delays, timing, event constructs, or multiple output arguments.
Functions are typically used for conversions and commonly used calculations.

The following lists are the same between tasks and functions:

  • Both tasks and functions must be defined in a module and are local to the module.
  • Tasks or functions cannot have wires.
  • Tasks and functions contain behavioral statements only.
  • Tasks and functions do not contain always or initial statements but are called from always blocks, initial blocks, or other tasks and functions.
  • Tasks and functions can have local variables, registers, time variables, integers, real, or events.

© 2024 Air Supply Information Center (Air Supply BBS)