## ByRef, ByVal, Sub, & Function

New to FreeBASIC? Post your questions here.
jfiofficial
Posts: 5
Joined: Nov 05, 2018 8:55

### ByRef, ByVal, Sub, & Function

what is the Simple and Easiest Explanation about ByRef & ByVal in FreeBASIC. I already read the FreeBASIC Manual but still don't get it.
the only basic that I know is the Simple Print, If-Else-Select Case, Do-Loop, For Loop & simple GUI using FbGfx.bi. thank you in advance :)
fxm
Posts: 9415
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: ByRef, ByVal, Sub, & Function

Have you read these documentation pages?
- Procedures Overview
- Passing Arguments to Procedures (read the paragraph 'Manually passing pointers to by-reference parameters' is optional, because it's just a punctual advanced feature)
- Returning Values (read the paragraph 'Manually returning pointers as-is from Byref functions' is optional, because it's just a punctual advanced feature)
Last edited by fxm on Jun 26, 2019 11:07, edited 2 times in total.
jfiofficial
Posts: 5
Joined: Nov 05, 2018 8:55

### Re: ByRef, ByVal, Sub, & Function

not yet. but ill check it. newbie here. thank you fxm
UEZ
Posts: 360
Joined: May 05, 2017 19:59
Location: Germany

### Re: ByRef, ByVal, Sub, & Function

Here a simple example using function only. It's the same with sub except there is no return value.

Code: Select all

`Function TestVal(x As Integer) As Integer   x += 1   Return xEnd FunctionFunction TestRef(ByRef x As Integer) As Integer   x += 1   Return xEnd FunctionDim As Integer x = 0? "Value x: " & x, "Return value from function: " & TestVal(x)? "Value x after function call: " & x?x = 0? "Value x: " & x, "Return value from function: " & TestRef(x)? "Value x after function call: " & xSleep`
MrSwiss
Posts: 3332
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: ByRef, ByVal, Sub, & Function

What is the simple and easiest explanation about ByRef & ByVal in FreeBASIC.
ByVal: the procedure (Sub/Function) gets a copy of the original variable.
(original remains "as-is", unchanged even if modified inside procedure!)
ByRef: the procedure (Sub/Function) references the original variable.
(changes made to it, remain permanent)

### Who is online

Users browsing this forum: No registered users and 1 guest