Sunday, October 11, 2015

Function and Task Argument Passing

Argument passed by value : Argument passed by value create copy in sub routine area.  That value changed in task or function will not visible outside that task or function,

Argument passed by reference : Argument passed by reference will share the same memory with the caller and sub routine area. Any changed made to the variable passed, will visible to caller as well sub routine.

class pass_by_value_n_ref;
  function pass_by_ref(ref int referance);
    referance ++;
    $display("from function pass_by_ref %0d",referance);
  endfunction
  function pass_by_value(int value);
    value ++;
    $display("from function pass_by_value %0d",value);
  endfunction
endclass

module top();
  int referance =10;
  int value =15;
  pass_by_value_n_ref pass_by_value_n_ref;
  initial
    begin
      pass_by_value_n_ref = new();
      $display("from module pass_by_ref before call %0d",referance);
      pass_by_value_n_ref.pass_by_ref(referance);
      $display("from module pass_by_ref after call %0d",referance); // Value Updated
      $display("from module pass_by_value before call %0d",value);
      pass_by_value_n_ref.pass_by_value(value);
      $display("from module pass_by_value after call %0d",value); // Same value
    end 
endmodule


OUTPUT :
from module pass_by_ref before call 10
from function pass_by_ref 11
from module pass_by_ref after call 11
from module pass_by_value before call 15
from function pass_by_value 16
from module pass_by_value after call 15