Could you explain function declaration?

Guest
Hi,
I am new to verilog. On a tutorial, it says: The differences between tasks and functions are reflected in the way they are defined and invoked. First of all a function returns a single result that is available through the function's invocation. The requires an assignment to a local variable that has the same name as the function itself. Any other assignment target in local variables cannot return more than one output.


I do not understand the last two lines. What is "the same name" means in the following?

"The requires an assignment to a local variable that has the same name as the function itself."

If there is function:

function [31:0] Factorial;
input [3:0] op1
reg [3:0] in;
begin
Factorial = 1;
for (in=2; in<=op1; in=in+1)
Factorial = in * Factorial;
end
endfunction

....
wire w_in;
net r_tmp;

r_tmp=Factorial(r_in);

What is the local variable? It is r_tmp in the above example?


Second question, what is "other assignment target"?

Thanks in advance.
 
On 28/03/14 19:01, rxjwg98@gmail.com wrote:
Hi,
I am new to verilog. On a tutorial, it says: The differences between tasks and functions are reflected in the way they are defined and invoked. First of all a function returns a single result that is available through the function's invocation. The requires an assignment to a local variable that has the same name as the function itself. Any other assignment target in local variables cannot return more than one output.


I do not understand the last two lines. What is "the same name" means in the following?

"The requires an assignment to a local variable that has the same name as the function itself."

If there is function:

function [31:0] Factorial;
input [3:0] op1
reg [3:0] in;
begin
Factorial = 1;
for (in=2; in<=op1; in=in+1)
Factorial = in * Factorial;
end
endfunction

...
wire w_in;
net r_tmp;

r_tmp=Factorial(r_in);

What is the local variable? It is r_tmp in the above example?

No, in the example the local variable is Factorial (e.g. in the line
Factorial = 1;).

Second question, what is "other assignment target"?
I think what that is trying to say is that you can't return two items at
once. In a task you can declare two (or more) output arguments. In a
function there are no output arguments, so you just have the one
variable to assign to (which has the same name as the function).

regards
Alan

P.S. It's all much more sensible in SystemVerilog. They even have
(gasp!) a return statement!


Thanks in advance.

--
Alan Fitch
 

Welcome to EDABoard.com

Sponsor

Back
Top