' ref '

what is the difference between ref and const ref in a function

In reply to suresh M:

Hi Suresh,

Please find example on provided link to understand difference betweenref and const ref.
Difference between ref and const ref

Hi Suresh,

Example in above comment explains it all, the main difference lies in the usage. If you want that calling function/task shall be able to modify the passed variables use only ‘ref’ other use ‘const ref’. By declaring a variable ‘const ref’ you make it read only for the respective function/task.

Thanks,
Rohit

In reply to suresh M:

Hi,

ref is writable and const ref is read only. whenever you pass ref argument in a task/function, the changes you do to that argument are reflected wherever it is used, whereas if it is constref, you just need to use the value cannot modify it.

In reply to suresh M:

From Const ref - SystemVerilog - Verification Academy

You first have to understand that by default, most task/function (routine) arguments are inputs copied by value. That means if you pass a variable to a task/function (the actual argument), the value of that variable is copied to a local variable upon entry to that routine (the formal argument). You can modify the formal argument, but the actual argument will never change, even after you return from the routine.
The reverse is true for an output argument. Upon entry to the routine, there is no change to the formal argument, but once inside, you can modify the formal, and the value of the formal argument gets copied to the actual argument. SystemVerilog also has an inout argument that copies the actual to the formal upon entry, and then copies the formal to actual upon return from the routine. A key point about copy by value, is that there is only one copy in, and and one copy out. If you have a time consuming task, the actual and formal arguments are not synchronized except upon entry and return from the routine.
A ref argument is always synchronized because you are passing a reference to the actual argument. When you refer to the formal argument inside your routine, you are referencing the actual argument, not a local variable. Another reason people use a ref argument is for performance instead of passing a large structure or an array when only a small number of elements need to be accessed. Most compilers do this automatically as an optimization, so this is usually not needed.
Finally, to get to your original question, a const ref argument behaves the same as a ref argument, except that the compiler will treat the formal argument as read-only. There are a number of situations that require functions to have no side-effects (no modification of variables outside of the function except through the return value). So the LRM restricts functions in this situation to only having input or const ref arguments.
Note that you almost never need to pass a class variable or virtual interface variable by reference since the handle is already a reference to an object. And a const ref argument of this type will only prevent you from changing the handle to point to another object. It will not prevent you from writing to a member of the class object.