{{fbdoc item="title" value="BYREF (function results)"}}----
Specifies that a function result is returned by reference

{{fbdoc item="syntax"}}##
	[[KeyPgFunction|Function]] //name// ( //parameter-list// ) **Byref** [[KeyPgAs|As]] //[[DataType|datatype]]//
##
{{fbdoc item="desc"}}
	Causes the function result to be returned by reference, rather than by value. A function returning ##**Byref**## will return the address of a variable, instead of making a copy like when returning by value. This allows the caller of the function to modify the variable which the function result points to.
	
	If ##**Byref**## is not specified, the default is to return the function result by value.
	
	Functions with ##**Byref**## result should not return local variables from the function, because they will be destroyed upon returning from the function, invalidating any pointer or reference to them. To help with writing safe code, the compiler will show an error message when a local variable is used with ##Function = //x//## (or ##//name// = //x//##) assignments and ##Return //x//## statements.
	
	**Note:** On the left-hand side of an assignment expression using the '##=##' symbol, the result of the function (returned by reference) must be enclosed in parentheses when the function calls one single argument, in order to solve the parsing ambiguity. '##=>##' can be used for assignments, in place of '##=##', same as for initializers, allowing to avoid parsing ambiguity (without parentheses). As for the arguments list, it should always be surrounded with parentheses even if empty.
	
	Operators (member or global), when used as functions, have also the capability to return results by reference, by using the same syntax.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/procs/byref-result4.bas"}}%%(freebasic)
Function min( Byref I As Integer , Byref J As Integer ) Byref As Integer
	'' The smallest integer will be returned by reference, no copy will be created.
	If I < J Then
		Return I
	Else
		Return J
	End If
End Function

Dim As Integer A = 13, B = 7
Print A, B
Print min( A , B )
min( A , B ) = 0
Print A, B
%%
{{fbdoc item="filename" value="examples/manual/procs/byref-result.bas"}}%%(freebasic)
function f( ) byref as const zstring
	'' This string literal (because statically allocated in memory) will be returned by reference, no copy will be created.
	function = "abcd"
end function

print f( )
%%
{{fbdoc item="filename" value="examples/manual/procs/byref-result2.bas"}}%%(freebasic)
Dim Shared As String s

Function f1( ) ByRef As String
   '' This variable-length string will be returned by reference, no copy will be created.
   Function = s
End Function

Function f2( ByRef _s As String ) ByRef As String
   '' This variable-length string will transit by reference (input and output), no copy will be created.
   Function = _s
End Function

s = "abcd"
Print s

f1( ) = f1( ) & "efgh"
Print s

'' The enclosing parentheses are required here on the left-hand side.
( f2( s ) ) = f2( s ) & "ijkl"
Print s

'' The enclosing parentheses are not required here on the left-hand side.
f2( s ) => f2( s ) & "mnop"
Print s
%%
{{fbdoc item="filename" value="examples/manual/procs/byref-result3.bas"}}%%(freebasic)
Function power2( Byref _I As Integer ) Byref As Integer
   _I *= _I
   '' This integer will be returned by reference, no copy will be created.
   Function = _I
End function

Dim As Integer I = 2
power2( power2( power2( I ) ) )  '' Function return-byref cascading is equivalent to ((I*I)*(I*I))*((I*I)*(I*I)) = I^8
Print I
%%

{{fbdoc item="diff"}}
	- New to ""FreeBASIC""

{{fbdoc item="see"}}
	- [[ProPgReturnValue|Returning values]]
	- ##[[KeyPgByref|Byref (parameters)]]##
	- ##[[KeyPgByrefVariables|Byref (variables)]]##

{{fbdoc item="back" value="CatPgProcedures|Procedures"}}