Functions with return and output arguments

What is the difference between function with return and function with output parameter?
When to use which type?

Thanks

In reply to shatrish:

Return values and output arguments are independent features of a function.

You call functions with return values as part of an expression, or as an expression by itself.


function bit my_function;
 return ...;
endfunction
if (my_function() !=0) ...

You call functions with no return value as a procedural routine that is guaranteed not to consume time (as opposed to a task, which is allowed to consume time).

In either case, you could have output arguments to your function. But here are some things to consider. When using an output argument, you must declare a variable to receive the value. \

bit test;
function void my_function(output bit value);
   value = ...
endfunction
...
my_function(test);
if (test) ...

If all you want to do is test the value, it is much easier to use the return value. However, there is only one return value for a function; so if you have more than one value to return, you must use output arguments.

There are a number of places in the LRM (e.g. using a function in a random constraint) that restrict the use of output arguments. These are so-called pure functions where return value must be solely defined as a result of the inputs to a function.

1 Like