module top_tb;
function static f( input int d_in );
int d = d_in + 1;
$display( "d_in = %d", d_in );
$display( "d = %d", d );
endfunction
initial
begin
f( 100 );
end
endmodule
I got next output:
> run
# d_in = 100
# d = 1
Why does “d_in” var is equal to 0 in “d” declaration/initialization line?
Is it correct?
The code you wrote is illegal to prevent the exact confusion you are experiencing. It should have generated a compiler error. [Edit: this code was legal for a short time in the IEEE 1800-2005 LRM]
Variables declared in a static task, function, or procedural block default to a static lifetime and a local scope. However, an explicit static keyword shall be required when an initialization value is specified as part of a static variable’s declaration to indicate the user’s intent of executing that initialization only once at the beginning of simulation.
This is because when in comes to the lifetime of local variables, legacy Verilog has its defaults backwards from almost every other programming language. SystemVerilog fixed this for class methods by making all local variables automatic by default, but could not change the behavior of legacy Verilog code.