Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
About The Best Formula In The World
Architecture details
By Reference Argument Passing
Compatibility between versions
Creating And Using Libraries
Database Datatype Mapping
Database Request Quoting
Date & time management
Dates and calendars
DBus and Gambas
Differences Between Shell And Exec
Differences From Visual Basic
Distributions & Operating Systems
Drag & Drop
DrawingArea Internal Behaviour
External functions datatype mapping
Frequently Asked Questions
Gambas Farm Server Protocol
Gambas Mailing List Netiquette
Gambas Markdown Syntax
Gambas Naming Conventions
Gambas Object Model
Gambas Scripting
Gambas Server Pages
Gambas Unit Testing
Gambas Wiki Markup Syntax
Getting Started With Gambas
Hall Of Fame
Image Management In Gambas
Including Help Comments in Source Code
Interpreter limits
Just In Time Compiler
Just In Time Compiler (old version)
Localisation and Internationalization
Mailing Lists & Forums
Naming Conventions
Network Programming
ODBC Component Documentation
PCRE Pattern Syntax
Porting from Gambas 2 to Gambas 3
Previous News
Project Directory Structure
Release Notes
Reporting a problem, a bug or a crash
Rich Text Syntax
Text highlighting definition file syntax
The Program has stopped unexpectedly by raising signal #11
Variable Naming Convention
WebPage Syntax
Web site home page
What Is Gambas?
Window & Form Management
Window Activation & Deactivation
Window Life Cycle
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Search the wiki
To Do
Wiki License
Wiki Manual

By Reference Argument Passing

The BYREF feature was mainly added to help people porting VB project.

It does not work by passing pointers, but by not releasing the value of the argument when the function ends, and put it into the expression passed by reference.

In other words:

GetData(ByRef sResult)

does actually the following:

GetData(sResult) ' Push sResult on the stack as first argument.
...              ' Do not free the stack after GetData ends.
sResult = ...    ' Gets the value from the stack and put it into sResult.

Note that this way, any assignment expression can be passed by reference.

GetData(ByRef MyCollectionOfLabels["key"].Text)

At function declaration, ByRef means that you can pass the argument by reference, but you don't have to.

At function call, ByRef means that you want the argument to be passed by reference.

As Gambas linking is entirely dynamic, the interpreter checks at runtime that you use ByRef if the function really allows it. This is the reason why ByRef must be specified both at function declaration and at function call.

ByRef makes the function call slower because of the needed checks and the added process of recalling the value from the stack.

ByRef is not supported by the JIT compiler at the moment.