BASIC | ByRef or ByVal

This example is the way that I’ve been using most for I’d like to maintain the value of x.

Is this function ByVal ?

Sub Is_this_ByRef_or_ByVal
	Dim x%
	x = 10
	MsgBox	"Is this ByRef or ByVal ?"									& Chr(13)	& Chr(13)	&	_
			"Sub Is_this_ByRef_or_ByVal"											& Chr(13)	&	_
			"    Dim x%"			 												& Chr(13) 	& 	_
			"    x = 10" 															& Chr(13)	&	_
			"    MsgBox ''Triple(x) = '' & Triple(x) & Chr(13) & ''x = '' & x"		& Chr(13)	&	_
			"End Sub"													& Chr(13)	& Chr(13)	&	_
			"Function Triple%(x%)"						 							& Chr(13) 	& 	_
			"    Triple = x * 3"													& Chr(13) 	& 	_
			"End Function"												& Chr(13)	& Chr(13)	&	_
			"Result :"																& Chr(13) 	& 	_
			"    Triple(x) = " & Triple(x)											& Chr(13) 	& 	_
			"    x = " & x
End Sub	

Function Triple%(x%)
	Triple	= x * 3
End Function

Does it make a difference in your function?

As far as I can recall from StarBasic coding (and other BASIC dialects I have touched), all parameters are passed by value. I believe some dialects provide a by-ref construct to override this.

It may make a difference with recursion, if you make assignment to the parameter variable, and reference in “mixed scopes”. I fail to see where your sample code does that.

Dear @keme,

Thank you for your comments. At first, I was not sure about the default parameters. I have to look back to this point for I am going to work with passing an array to Function and return an array for the result.

Then I understand your question better. Array parameters and array return values may give rise to performance issues when passed by value.

I recall that one system would default to by-value for atomic data but by-ref for structured data. It also allowed for passing as copy-restore. It was not prominently present in the documentation, so it cost me some time and a few hairs pulled before I figured it out. Can’t remember now what it was called (didn’t happen in this century). Not even sure what was the language, but I would guess some offbeat COBOL variety.

Hello,

Just for reference, from the StarOffice ™ 6.0 Basic Programmer’s Guide:

Parameters are normally passed by Reference in StarOffice Basic.

It then goes on to state:

You can also pass a parameter as a value if you do not want subsequent changes to the parameter to
affect the value that is originally passed. To specify that a parameter is to be passed as a value,
ensure that the ByVal keyword precedes the variable declaration in the function header.

PDF here

Edit:

This post by @Lupp may also be of interest → BASIC: ByVal and arrays

Thanks to Ratslinger. I stand corrected.

Been a while since I did anything in StarBasic. I was quite sure that at the time the passing was by value. Now I am less certain.

Too lazy to check the docs. :blush:

Passing arrays or other structures is a similar problem in many languages, not only Basic. Whether the reference itself is passed by reference or by value matters little, because the contents of the array will be the same either way. If you change the passed array, then the original array is changed as well.

The following code displays “banana”, so ByVal is misleading and should not be used here.

Sub CallModifyArray
   Dim a(0)
   a(0) = "apple"
   Call ModifyArray(a)
   MsgBox a(0)
End Sub

Sub ModifyArray(ByVal a)
	a(0) = "banana"
End Sub

To keep the original from getting changed, you must make a copy of the array. @Lupp gives some Basic code to do this (from the comment by @Ratslinger): BASIC: ByVal and arrays - #4 by Lupp.

Other languages such as Python make this easier, where a copy of an array can be made with b = a[:] or b = copy.deepcopy(a). Sometimes it’s necessary to implement __deepcopy__ to copy an object fully, so you have to know what the data is going to contain.

In C, const parameters can be used to indicate that the function will not change the array.