Page 1 of 1

byRef!

Posted: Thu Apr 22, 2010 11:54 pm
by peter
Hello Zero,

One question!
When I pass a value to a Function as Parameter, is this value "byRef" or "byVal" ?

Regards.

Re: byRef!

Posted: Fri Apr 23, 2010 1:01 am
by SPuntte
Hi peter,
I guess you are asking whether CoolBasic can pass references of variables to some other scope (eg. a function). I hope I didn't guess wrong :P

The short answer is: NO. And that's because CoolBasic has no such a feature as a reference - or anything that has something to do with pointers and memory addresses explicitly.

And a bit longer answer is still the same, but with a code snippet :P

Code: Select all

//Here we are in the "main program" scope

//Let's define a variable
Dim someValue As Integer
someValue = 42

//Let's define a variable with custom structured type
Type MY_OWN_TYPE
    Field intVal As Integer
EndType
var.MY_OWN_TYPE = New(MY_OWN_TYPE)
var\intVal = 42

//Here we have two functions which have their own scopes and local variables.
Function doSomeMagic(var As Integer) //The parameter is a value, not a reference.
    Print "Function doSomeMagic() called."
    var = var + 1 //This has NOTHING to do with ANY OTHER variable caller 'var' in this program. It's totally local in this funcion scope.
EndFunction

Function doRealMagic(varID As Integer) //This parameter is also a simple value, though it acts a bit as a real reference
    Print "Function doRealMagic() called."
    var.MY_OWN_TYPE = ConvertToType(varID)	//As the "ID" of the structured type instance is passed to the Function scope, the original value can actually be accessed.
    var\intVal = var\intVal + 1
EndFunction

//First print the variables to see the initial values
Print "Value of someValue is " + someValue + "."
Print "Value of var\intVal is " + var\intVal + "."
Print ""

//Then do some magic ;)
doSomeMagic(someValue)
doRealMagic(ConvertToInteger(var))

//Print the info again
Print ""
Print "Value of someValue is " + someValue + "."
Print "Value of var\intVal is " + var\intVal + "."

WaitKey
In conclusion, references do not exist in CB. However, as I showed in the snippet, custom types can mimic the properties of a reference. Along memory blocks they are actually the only way to handle generic (ie. without knowing the exact variable name which is the case when using global variables or arrays that are automagically global in CB) data which has been allocated outside the current scope.

I'm eager to answer any futher questions should you have some :)

Re: byRef!

Posted: Fri Apr 23, 2010 7:39 am
by Zero
As a short summary (in addition to SPuntte's clarifying explanation):

In CoolBasic all function arguments are passed by value. This means that if you pass in a variable its value is duplicated for the function, and changing the function parameter variable inside the function doesn't reflect back to the callee side.

In some other programming languages, such as Visual Basic and C++, it's possilbe to pass arguments by reference, meaning that if you pass in a variable, changing the corresponding parameter variable value inside the function will also modify the value of the original variable that was passed to the function. For now, CoolBasic doesn't support passing by reference.

As SPuntte demonstrated, it's however, possible to return data in a similar manner to reference variables, but you have to allocate, manage, and free a memblock or typed variable manually.

Re: byRef!

Posted: Fri Apr 23, 2010 1:10 pm
by peter
Hi,

Your explanations have helped me!
Thank you, both.

Best regards