Variable Declaration inside Function

In reply to PR359708:

In modules, functions are static by default, meaning variables declared in them will be static variables. So, in scenarios 1 & 2, the function is static. Consequently, the initialization in scenario 2 is only executed once (it is a static initialization completed before your initial block executes). In scenario 1, the variable is also static, but you using are procedurally computing the value when the function is called. In scenario 3, variable c is an automatic variable, so the initialization executes on every function call.

To make the effect more obvious, try converting this function into some kind of recursive function that calls itself, eg.

module mm;
  function int s;
    input int a;
    input int b ;
    int c = a+b;
    //c= a+b;
    if ( a > 0 ) s( a-1, b);
    $display("a=%d -- b=%d --  c=%d ",a,b,c);
    return c;
  endfunction

This will demonstrate the problem more clearly.