ByRef, ByVal, Sub, & Function

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

ByRef, ByVal, Sub, & Function

Post by jfiofficial »

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

Re: ByRef, ByVal, Sub, & Function

Post by fxm »

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

Post by jfiofficial »

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

Re: ByRef, ByVal, Sub, & Function

Post by UEZ »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ByRef, ByVal, Sub, & Function

Post by MrSwiss »

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)
Post Reply