Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Components
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
About The Best Formula In The World
Architecture details
Benchmarks
Books
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
Housekeeping, cleaning up
Image Management In Gambas
Including Help Comments in Source Code
Interpreter limits
Introduction
Just In Time Compiler
Just In Time Compiler (old version)
License
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
Screenshots
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
XML APIs
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
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.