Below two different coding style gives different results while using inout
in automatic
task
.
Style 1:
task automatic sum (input int x, input int y, inout int z);
#10;
z = x + y ;
endtask
int out;
initial begin
fork
sum(1,2,out);
join_none
#15;
$display("sum output is %0d",out); // Printing the expected value of 3
end
Style 2 :
task automatic sum (input int x, input int y, inout int z);
fork
begin
#10;
z = x + y ;
end
join_none
endtask
int out;
initial begin
sum(1,2,out);
#15;
$display("sum output is %0d",out); // Printing the value of 0
end
Can someone please explain why "Style 2" gives the value of 0?
At time 0, you call the sum
task, and the inout
port is assigned the value 0 since it is an int
(whose default value is 0). The task
forks off a process, then returns immediately due to the join_none
, still at time 0. So the value of z
is passed to the out
variable as 0.
out
does not receive the value of z
after a delay of 10 or 15.
Refer to IEEE Std 1800-2023 section 13.3 Tasks:
inout // copy in at beginning and out at end