Calling task inside function

I was asked question in interview. Is it possible to call a task(time consuming method) inside a function(executed in 0 time)?

I said no, but interviewer said there is a method to do that in system verilog. Whats that method??Please help me with answer to this.

You can only do it indirectly from within a process spawned by a fork/join_none.

See section 13.4.4 Background processes spawned by function calls of the 1800-2012 LRM

In reply to manavshah33:

I am not sure if there is a separate method in System Verilog.
One way to call a task inside a function using fork - join_none

task a;
#100;
endtask

function void b();
fork
a
join_none
endfunction

In reply to prachi01:

Please use code tags.

Your code does not call function b anywhere.

In reply to prachi01:

In reply to naveensv:
I’m not able to call task from function using fork/join_none.
function_calling_task - EDA Playground
Could someone help to explain why I don’t see display statement. I will appreciate the help.


module test;
task xyz;
#100;
$display(“I’m in xyz\n”);
endtask
function void b;
fork
xyz;
join_none
endfunction
// put these lines
initial begin
b;
end
endmodule


In reply to DKVerif:

i checked your code in ncsim .it is working fine.

!(file:///C:/Users/hp33493/Desktop/Capture)

In reply to dave_59:

Hi Dave,

could you give some use-cases for calling tasks within functions?

Thanks

In reply to abdulmutaalahmad:

Hi Abdul,
I used it in a Scoreboard. Inside the virtual write function. I have put a task under fork - join_none, which will go and check and do data comparison of all bursts for each request.

In reply to dave_59:

Hi Dave,

Looks like this link is not valid anymore. Can you please point to an active link?
If not please let me know if there is a way we can call a task from within a function with an example.

Thanks.

In reply to pare:

That’s not a link, just a reference to a section in the LRM.

In reply to dave_59:

Hi,

Below code is working, but I was expecting a task (time consuming) can’t be called inside a function. Does that matter how function is getting called?


module tb;
  initial 
    repeat(2)
      begin 
        fork 
          functio_n;
        join
      end

  function functio_n;
    $display($time, "\t function");
    tas_k;
  endfunction

  task tas_k;
   $display($time, "\t task");
  endtask
endmodule

In reply to mpattaje:

In reply to dave_59:
Hi,
Below code is working, but I was expecting a task (time consuming) can’t be called inside a function. Does that matter how function is getting called?


module tb;
initial 
repeat(2)
begin 
fork 
functio_n;
join
end
function functio_n;
$display($time, "\t function");
tas_k;
endfunction
task tas_k;
$display($time, "\t task");
endtask
endmodule

This task isn’t time consuming. so it’s expected to work fine.
No, it doesn’t matter how function is getting called.


  task tas_k;
   $display($time, "\t task");
  endtask

In reply to Rahulkumar:
That code is illegal.

The Current LRM says:

Functions have restrictions that make certain they return without suspending the process that enables them. The following rules shall govern their usage, with exceptions noted in 13.4.4:

  1. A function shall not contain any time-controlled statements. That is, any statements containing #, ##, @, fork-join, fork-join_any, wait, wait_order, or expect.
  2. A function shall not enable tasks regardless of whether those tasks contain time-controlling
    statements.

The noted exceptions are statements inside a fork/join_none.

Some tools only apply restriction a) because in Verilog, there was no way to write a function without it being part of an expression, so there a people still writing tasks when they should be using function void.

But you are setting yourself up for a maintenance nightmare if you have several levels of nested functions and tasks calls, and someone introducers a blocking construct inside a deeply nested task.

So use a function when you want a guarantee that the procedure does not block.