Skip to content

Verilog · Chapter 15.1 · Tasks & Functions

Functions in Verilog — Reusable Combinational Computation

A function packages a reusable computation that returns a single value, and it is the tool for repeated combinational logic within a module. It takes one or more inputs and no outputs, contains no timing controls, executes in zero simulation time, and assigns its result to the function's own name. Because it is constrained this way, a function is combinational and synthesizable, and it is called inside an expression wherever you need its value. Functions are the everyday answer to repeated computation, such as a parity calculation used in several places, a custom encoding or decoding, a sign extension, or an address comparison. This page drills the function, covering its definition, the rule that the result returns through the function name, the constraints that make it combinational, and how it is called and synthesized.

Foundation12 min readVerilogfunctionCombinationalReuseRTL Design

Chapter 15 · Section 15.1 · Tasks & Functions

1. The Engineering Problem

The same computation appears in several places — and copy-pasting it is a maintenance trap. A function packages it once:

A function returns a single value, takes inputs only, has no timing, and is called in an expression — reusable combinational computation, synthesizable.

This page drills the function and its rules.

2. Mental Model — A Value-Returning Combinational Subroutine

3. Defining a Function

function-def.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   // returns the parity of an 8-bit value (result assigned to the name):
   function parity;
       input [7:0] data;
       parity = ^data;              // assign result to the function name
   endfunction
 
   // ANSI-style with a return width (Verilog-2001):
   function [3:0] add4 (input [3:0] a, b);
       add4 = a + b;
   endfunction
  • The result is assigned to the function name (parity = ..., add4 = ...).
  • A function has at least one input and no outputs/inouts.
  • The return width is declared after function (function [3:0] add4 returns 4 bits; a bare function name returns 1 bit).
  • Functions use blocking = (combinational) and cannot contain timing controls or call tasks.

4. Calling a Function

function-call.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   // in a continuous assignment:
   assign p = parity(bus);
 
   // in a procedural block:
   always @(*) sum = add4(x, y) + add4(z, w);   // called twice, in an expression
 
   // nested / composed:
   assign result = add4(add4(a, b), c);

A function is called inside an expression, anywhere a value is needed — continuous assignments, procedural blocks, other function calls. Each call site synthesizes the function's logic (the tool may share or replicate it). Because it returns instantly with no timing, it composes freely in expressions.

Visual A — a function returns a value into an expression

function — inputs in, one value out, in an expression

data flow
function — inputs in, one value out, in an expressioninputsdatafunction (notiming)combinational, zero-timeone return valueassigned to the nameused in anexpressionassign p = f(data);
A function takes inputs, computes a single value with no timing (combinational, zero simulation time), and returns it via the function name — used inside an expression. It is the reusable-computation tool, synthesizing to combinational logic at each call site.

5. The Function Rules

The constraints that make a function combinational:

  • Exactly one return value (via the name); for multiple outputs, use a task.
  • Inputs only — no outputs/inouts.
  • No timing controls (#, @, wait) — executes in zero time.
  • Cannot call tasks (tasks may have timing); can call other functions.
  • Use blocking = inside (it is combinational).

Breaking these is a compile error (e.g. a # delay in a function), which keeps functions clean and synthesizable.

6. Common Mistakes

  1. Timing in a function — illegal; functions have no timing (use a task) (§5, DebugLab 1).
  2. Multiple outputs from a function — a function returns one value; use a task for many (§2).
  3. Calling a task from a function — illegal; functions can call only functions (§5).
  4. Forgetting to assign the function name — the return value comes from assigning the name (§3).

7. Debugging Lab

One function debug post-mortem

8. Interview Q&A

9. Exercises

Exercise 1 — Write a function

Write a function sign_extend that takes a signed 8-bit input and returns a 16-bit sign-extended value.

Exercise 2 — Function or task?

For each, choose function or task: (a) compute the parity of a word; (b) drive a multi-cycle bus transaction; (c) return the max of two values; (d) apply reset stimulus over several cycles.

Exercise 3 — Spot the error

Why does a function with @(posedge clk) inside fail, and what should you use instead?

10. Summary

A function is reusable combinational computation:

  • Returns one value (via the name), inputs only, no timing, zero-time — used in an expression.
  • Combinational and synthesizable — a pure function of its inputs.
  • Rules — one return, no outputs/inouts, no timing, can't call tasks, blocking =.
  • Use for repeated value-returning logic (parity, encode, decode, sign-extend).

The next sub-topic is the procedure construct: Chapter 15.2 Task drills tasks — any I/O, optional timing, called as a statement.