byRef!

Post your coding questions such as 'how to' here.
Post Reply
peter
Active Member
Posts: 123
Joined: Mon Oct 22, 2007 2:31 pm

byRef!

Post by peter » Thu Apr 22, 2010 11:54 pm

Hello Zero,

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

Regards.
Last edited by peter on Fri Apr 17, 2015 1:34 am, edited 1 time in total.

User avatar
SPuntte
Tech Developer
Tech Developer
Posts: 650
Joined: Mon Aug 27, 2007 9:51 pm
Location: Helsinki, Finland
Contact:

Re: byRef!

Post by SPuntte » Fri Apr 23, 2010 1:01 am

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 :)
CoolBasic henkilökuntaa
Tech-kehittäjä
CoolBasic Classic, Cool VES

CoolPhysicsEngine | MissileSystem | Jana-ympyrä -törmäys | cbSimpleTexture | CoolCPLX

User avatar
Zero
Lead Developer
Lead Developer
Posts: 712
Joined: Sun Aug 26, 2007 2:30 pm
Location: Helsinki, Finland
Contact:

Re: byRef!

Post by Zero » Fri Apr 23, 2010 7:39 am

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.
CoolBasic henkilökuntaa
Johtava Suunnittelija
CoolBasic V3, CoolBasic Classic

http://www.coolbasic.com/blog

peter
Active Member
Posts: 123
Joined: Mon Oct 22, 2007 2:31 pm

Re: byRef!

Post by peter » Fri Apr 23, 2010 1:10 pm

Hi,

Your explanations have helped me!
Thank you, both.

Best regards

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests