Repository navigation
Byref Function Results
Home › Procedures › Byref (Function Results)
Specifies that a function result is returned by reference
Function name ( parameter-list ) ByRef As datatypeCauses 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.
- New to 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, BFunction 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( )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 sFunction 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 ICategory: Procedures
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing