Ref keyword significance for class type varaible

Need some help on understanding following code. what is need for ref keyword for ref_dst in the following code ? why can’t simply pass the value to copy function where as I notice that whenever passing object ( after constructed) to another function/task to ref__ becomes not a requirement.


class Transaction;
bit [31:0] addr=100, data=16’hAAAA;

function void copy(ref Transaction ref_dst); // what is need for ref here ?
$display(“I’m in copy function\n”);
ref_dst=new();
ref_dst.addr = addr;
ref_dst.data = data;
endfunction
endclass

module top;
Transaction src, dst;
initial begin
src = new();
$display(“src.addr %2d src.data=%2x”,src.addr,src.data);

src.copy(dst);
$display("dst.addr %2d dst.data=%2x",dst.addr,dst.data);

end
endmodule


In reply to prachi01:

This was probably written by a C/C++ coder. There is no need for a ref argument here. It should be an output. (and the function is doing more of a clone, not just a copy.

There is almost never a need to pass a class variable by reference - it is already a reference to an object. You want to copy the value of the class handle that is constructed inside the function, and copy the handle to the class variable dst in the module when the function returns. The previous reply is incorrect: it is the handle to the object that gets passed through the argument, not the object itself.

And there is almost never a need to use ref arguments with a function. The only time you might consider passing by reference is when an argument is a large array or structure and you want to avoid having a local variable that needs to copy the entire array/struct for better performance. But accessing an argument by an indirect reference is more expensive than a direct local access. So if your function does a lot of accessing that argument, you will lose all the performance gain you were hope to get. So it’s best to use an input/output/inout argument in a function a let the compiler optimize the code for you.

In reply to dave_59:
Thank you so much Dave for a correction. I have removed my previous reply.

In reply to dave_59: Thank Dave for clear explanation. it clarified me motivation behind ref keyword.

In reply to prachi01:
Actually, the key motivation behind a ref argument is with a time consuming task where you need keep it up with changes. See

https://verificationacademy.com/forums/systemverilog/passing-arguments-reference#reply-38487

In reply to dave_59: Thanks Dave for additional information.