Forum Discussion
Altera_Forum
Honored Contributor
9 years agoLet's say you have this
package pkg;
int gacc;
function int fout(input int in);
int acc;
if (in !=0) begin
acc += in;
fout = acc+ gacc;
gacc = acc;
end
endfunction
endpackage
// in some code you have
$display(fout(1)); // prints 1
$display(fout(0)); // prints 1 - returns last value assigned to fout
$display(fout(1)); // prints 3 Those results are because gacc, add, in, and fout are all static variables initialized once at time 0. You can make a function automatic, which makes all arguments, return values, and local variables implicitly automatic (initialized upon each entry to the function) package pkg;
int gacc;
function automatic int fout(input int in);
int acc; // implicitly automatic
if (in !=0) begin
acc += in;
fout = acc+ gacc;
gacc = acc;
end
endfunction
endpackage
// in some code you have
$display(fout(1)); // prints 1
$display(fout(0)); // prints 0 // no value assigned to fout
$display(fout(1)); // prints 2 When you declare a package or module as automatic, then all named blocks, tasks, and functions declared in that package become implicitly automatic package automatic pkg;
int gacc; // always static - not inside any block
function int fout(input int in); // implicitly automatic
int acc; // implicitly automatic
if (in !=0) begin
acc += in;
fout = acc+ gacc;
gacc = acc;
end
endfunction
endpackage
// in some code you have
$display(fout(1)); // prints 1
$display(fout(0)); // prints 0
$display(fout(1)); // prints 2