xHarbour Reference Documentation > Operator Reference |
Pass-by-reference operator (unary): passes variables by reference.
@ <Variable>
When a variable is passed by reference to a subroutine, any changes made to the variable are reflected in the calling routine once the called routine has returned.
Normally, variables are passed by value. This means that the called routine receives a copy of the passed value so that the variable's value in the calling routine remains unaffected by assignment operations in the called routine.
When a variable is prefixed with the @ operator in a function call, the called function receives a reference to the variable. The variable can then be assigned a new value in the called function. The changed value is reflected in the calling routine when the called function has finished.
Passing variables by reference is usually required when a subroutine must deliver more than one return value.
See also: | FUNCTION, METHOD (Declaration), PROCEDURE |
Category: | Operators , Special operators |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example shows the difference between a function call with // and without the @ operator: PROCEDURE Main() LOCAL cMsg := "Hello World" ? GetMsg( cMsg ) // result: Ok ? cMsg // result: Hello World ? GetMsg( @cMsg ) // result: Ok ? cMsg // result: Hi there RETURN NIL FUNCTION GetMsg( cString ) cString := "Hi there" RETURN "Ok"
http://www.xHarbour.com