Work it through
A bounded launch
Use an automatic void function that forks worker() with join_none. Call it from initial, then wait fork; the worker waits 5 ns.
launch() consumes no simulation time; wait fork supplies the completion boundary.
Your next page is on its way.
Chapter 1 · Subroutines & lifetime
Choose the right subroutine contract, then make argument direction, storage lifetime, reentrancy, and side effects explicit.

A worked mechanism
The fork…join_none exception does not let a function block its caller.
t = 0
A side-effect-permitted initial process calls launch().
caller has not yielded
Inside function void launch(): fork worker(); join_none. The worker task may wait.
still t = 0
The function completes; the caller reaches wait fork.
t = 5 ns
The child starts when the caller blocks. A worker containing #5ns completes later.
Work it through
Use an automatic void function that forks worker() with join_none. Call it from initial, then wait fork; the worker waits 5 ns.
launch() consumes no simulation time; wait fork supplies the completion boundary.
A direct user-task call in the function remains illegal. The background exception requires a permitted procedural origin and side-effect context; never use it to hide work in an assertion or static initializer.
Log function return at 0 ns and worker completion at 5 ns. Keep automatic captures and explicit ownership of every child.
The language bench / lifetime
task automatic work(input int id);
int local_id = 0;
local_id = id;
#10;
$display(local_id);
endtask
// A enters at t0 with id=1; B enters at t1 with id=2.Question-first preparation
Each question maps directly to one of the sections below, so you can test the contract before reading the explanation.
One representative prompt from each major mechanism. Open any card in the live question bank.
Subroutine contract
The distinction is not stylistic: a function must complete without consuming simulation time, while a task can synchronize, delay, and return several results through arguments.
Subroutine selector
Zero-time computation that can appear in an expression.
Zero-time procedure, commonly used for model updates.
May wait, delay, synchronize, and use output/inout arguments.
Zero-time computation that can appear in an expression.
Design a subroutine like a small interface: inputs, outputs, time, side effects, and ownership must all be visible.
List outputs, side effects, timing controls, imported calls, and whether the caller uses the result inside an expression.
Choose a typed function or function void for zero-time work; choose a task when event controls, delays, or activity-style completion are part of the API.
Hiding time in a function breaks the expression model, while a task used as a pure calculator obscures ownership and makes composition harder.
Lint timing controls and side effects, unit-test automatic reentrancy, and assert the caller-visible postconditions at the API boundary.
A non-void function returns through its name or return statement and can participate in an expression. function void performs zero-time side effects without a value. A task represents an operation whose completion is a sequencing event for the caller.
function automatic logic [7:0] parity_mask(
input logic [63:0] data
);
logic [7:0] result;
foreach (result[i]) result[i] = ^data[i*8 +: 8];
return result;
endfunction
task automatic send_beat(
input logic [31:0] data,
output bit accepted
);
req <= 1'b1;
payload <= data;
do @(posedge clk); while (!ready);
accepted = 1'b1;
req <= 1'b0;
endtaskA statically bounded, zero-time task can be inlined by synthesis just as a function can. This describes analyzable behavior, not static storage lifetime; an automatic task can also synthesize. Delays, event controls, dynamic testbench objects, and simulator services make a subroutine testbench-only. Review the body and call context rather than declaring every task unsynthesizable.
| Question | Function | Task |
|---|---|---|
| Consumes time? | No | May |
| Used in expression? | Yes, if non-void | No |
| Return values | One typed value plus arguments | Arguments only |
| Direct synchronous calls | Functions only | Functions and tasks |
| Automatic recommended? | Yes for reentrancy | Yes for concurrent calls |
| Synthesizable? | If body is synthesizable | If behavior is statically bounded and zero-time |
System functions and array methods often look self-explanatory, but interview bugs hide in width, four-state treatment, sampling context, and whether the operation mutates its receiver. State those contracts before using the result.
| Operation | Contract | Common review point |
|---|---|---|
| $clog2(n) | Ceiling log2 as an integer | Guard n <= 1 and resulting zero-width declarations |
| $countones(expr) | Count bits that are exactly 1 | X and Z are not counted as ones |
| $onehot(expr) | Exactly one bit is 1 | Use $onehot0 when all-zero is legal |
| $onehot0(expr) | At most one bit is 1 | Unknown control bits still need an explicit policy |
| $cast(dst, src) | Checked runtime conversion | Test success before consuming dst |
| $sampled(expr) | Value sampled for the current assertion attempt | Do not confuse it with an arbitrary procedural read |
| array.shuffle() | Randomize element order in place | The receiver mutates; preserve a copy if order matters later |
| array.find_index with (...) | Queue of matching indices | It does not return matching element values |
Explain it out loud
A function must complete in zero simulation time and may return a value for an expression. A task may consume time and represents an activity; results travel through output or inout arguments.
The expected answer is about time and calling semantics, not just the number of outputs.
Saying tasks are always unsynthesizable or that a task has a void return type.
Not as a direct synchronous call, even if the task body has no timing control. The fork…join_none background-process exception permits task-legal child statements in a side-effect-permitted procedural context originating from initial, always, or their forked processes. The function itself still returns without delay.
Look for awareness of transitive timing, not only syntax in the immediate function body.
Assuming an apparently zero-time task is a portable escape hatch.
Arguments and storage lifetime
input, output, inout, ref, const ref, automatic, and static control copying, aliasing, initialization, and reentrancy. Those are observable API properties.
Call boundary
Callee reads a snapshot; later caller changes are not observed.
Formal is written locally and copied to actual on return.
Snapshot enters and final value returns.
Immediate shared object; const ref forbids callee writes.
Callee reads a snapshot; later caller changes are not observed.
Copy semantics and object-handle semantics coexist: the formal can be a copy while the referenced object remains shared.
For each formal, record whether the callee reads, writes, aliases, or retains it, plus whether concurrent invocations are possible.
Use value input/output for snapshot-and-return behavior, const ref for read-only aliasing, and ref only when changes must be immediate and type-exact.
A static local shared by overlapping calls creates a race; a ref formal can expose partial updates that a copy-out output would hide.
Run overlapping calls with distinct data, assert inputs remain unchanged for const APIs, and check that output values appear only at return when that is the contract.
Value input is initialized on entry. Output is copied back on return. Inout does both. ref aliases the actual for the entire call, so writes are immediately visible; const ref keeps the performance and aliasing of ref but prevents assignments through the formal. Because a ref formal aliases live caller storage, it is legal only in an automatic-lifetime subroutine.
function automatic int checksum(
const ref byte unsigned data[],
output bit valid
);
int sum = 0;
valid = (data.size() != 0);
foreach (data[i]) sum = (sum + data[i]) & 'hFFFF;
return sum;
endfunction
task automatic swap(ref int a, ref int b);
int tmp = a;
a = b;
b = tmp;
endtaskStatic subroutine storage persists for the containing scope and is shared among calls. Automatic storage is created for each invocation. Methods are normally automatic, but an explicit static local still shares state. Recursive and forked calls need automatic locals unless shared state is intentional and protected.
Explain it out loud
ref aliases the caller's variable for the duration of the call, so writes are immediate. output uses a local formal whose final value is copied back when the subroutine returns.
A strong answer explains timing of visibility, not just “reference versus copy.”
Using ref only as a performance optimization while ignoring caller-visible mutation.
Automatic gives each invocation independent arguments and locals, making recursion and overlapping calls reentrant. Static storage persists and is shared, which is useful only when that shared state is intentional.
Connect lifetime to races and reentrancy.
Assuming a static local is safe because each source call appears in a different block.