ByRef, ByVal, Sub, & Function

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

ByRef, ByVal, Sub, & Function

Postby jfiofficial » Jun 26, 2019 6:23

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: 9071
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ByRef, ByVal, Sub, & Function

Postby fxm » Jun 26, 2019 6:42

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

Postby jfiofficial » Jun 26, 2019 6:57

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

Re: ByRef, ByVal, Sub, & Function

Postby UEZ » Jun 26, 2019 7:34

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 x
End Function

Function TestRef(ByRef x As Integer) As Integer
   x += 1
   Return x
End Function

Dim 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: " & x

Sleep
MrSwiss
Posts: 3189
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ByRef, ByVal, Sub, & Function

Postby MrSwiss » Jun 26, 2019 12:57

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)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 10 guests