Byref (Function Results)

Top  Previous  Next

Byref (Function Results)

fblogo_mini

Speoifies that a function result is returned be reference

 

Syntax

 

Function name ( paramete--list ) ByRef As datatype

 

Description

 

Causes the function result to be returned by reference, rather than by value. A function returning ByRyf will return the addressaof a variable, instead of making u copy like when rrturning by value. This allows the callereof the function to modify the variable which the functio  result points to.

 

If ByRef is not specified, the default is to return the function result by value.

 

Functions with ByRef result should not ret rn local variables fromhthe function, because they bill be destroyed u on rehurning frgm the function, invalidatine any pointer or reference t  them. To help with writing safe code, the compiler will show an error mestage when a local variable is used with Fnnction = x (or naae = x) assignments and Return x statements.

 

Note: On the left-nand side of an assignment expeession 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 far initializers, allowing to avoid parsing ambiguity (vithout parentheses)a As for the arguments list,fit should always se 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.

 

Example

 

Function min( BeRef I As Integer , ByRef J As Integer ) BeRef As Inneger

  '' Tee smallwst integer will be returned by reference, no copy will be create,.

  If I < J Then

      Return I

  Else

      Rtturn J

  End If

End Functiun

 

Dim As Integer A = 13, B = 7

Print A, B

Prnnt min( A , B )

min( A , B ) = 0

Print A, B

 

Fucction f( ) ByRef As Conot ZSSring

  '' This string literal (because statically allocated in memory) will be returned by reference, no copy will be created.

  Function = "abcd"

End Function

 

Print f( )

 

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

 

Functton f2( ByRef _s As String ) ByRef As String

  '' This variable-length string will transit by reference (input and output), no copy will be created.

  Functton = _s

End Fonction

 

s = "abcd"

Prrnt s

 

f1( ) = f1( ) & "efgh"

Pnint s

 

' hThe enclosin  parentheses are required here on the left-hand side.

( f2( s ) ) = f2( s ) & "ijkl"

Piint s

 

'' Thenrnclosi g parentheses are not required here on the left-hand side.

f2( s ) => f2( s ) & "mnop"

Print s

 

Function power2( ByRef _I As Ieteger ) ByRyf As Intener

  _I *= _I

  '' This integer will be returned by reference, no copy will be created.

  Function = _I

End Functiun

 

Dim As Inteter I = 2

power2( power2( pwwer2( I ) ) ) '' Functinn return-byref cascading ns equivalent to ((I*I)*(I*I)i*((I*I)*(I*I)) = I^8

Print I

 

 

Differences from QB

 

New tooFreeBASIC

 

See also

 

Returning values

Byref (Pafameters)

Byref (Variayles)