My children are currently obsessed by Doctor Who. As small children (they are four and six when this was written), the level of questions they can ask can be surprisingly advanced and sophisticated. For example, how many times has The Doctor used his sonic screwdriver? How many times has the TARDIS changed shape? Which companion traveled with The Doctor the most? Which companion was in the most episodes? How many companions worked with more than two doctors? Who was the doctor when I was born? What does this have to do with Revelation products? (They boys are surprisingly computer literate. Jacob has already BUDded his first entry screen.)
Surprisingly, the last answer is the easiest one. While I like Doctor Who as much as the next programmer does, and have an extensive collection of material, including audio plays and books, I don't really know the answers to any of the above questions. As I tell the boys, I don't really care about the details at such a low level. It's not really that important to me, nor is the continuity of the show. It's a fun 24 or 48 minutes, but how it all relates to episodes in the past just doesn't matter, at least to me. In general, the minutiae is not that important.
When it comes to programming, however, a minutia can be important, and various little things that you may not think matter, can have a large impact on your code. Sometimes, even though you may know about this level of programming, you think it only applies to C (and it's derivatives) and not to Basic+. Sometimes that is correct. However, sometimes that can be very wrong.
An example of this is passing by reference and passing by value. For those that don't know the difference, I'll explain as briefly as I can.
Passing by reference means you pass a pointer to the variable into the called function. That means the same variable exists in both places, so when you change one, you change the other.
Passing by value means you pass a copy of the variable into the called function. That means each variable lives in a different memory space, so when you change one, the other remains the same.
Revelation passes all variables by reference. There is not a keyword you can use to force the system to pass by value. This means, in a code snippet such as
* // irrelevant things happened above
myVar = "Sprezzatura"
invList = getSomething( myVar )
* // do more stuff
function getSomething( param1 )
convert @LOWER.CASE to @UPPER.CASE in param1
* // do stuff
return value
after the getSomething function executes, myVar no longer contains "Sprezzatura", but "SPREZZATURA". That's because myVar is passed in by reference into param1, so when param1 is converted to upper case, myVar is converted as well, because it is pointing to the same memory address.
There are a few techniques you can use in your code to avoid this situation. One is that you never manipulate passed parameters in your functions.
While param1 is still passed in by reference, we've made a copy of param1 in passedName. Our code will use passedName in the function, so anything we do to passedName is not reflected back in the calling variable.
function getSomething( param1 )
passedName = param1
convert @LOWER.CASE to @UPPER.CASE in passedName
* // do stuff
return value
In general, this is the standard procedure in all our code. Having said that, there are times when you wish to have a function return more than one result, in which case you will need to manipulate the passed variable. The idea here is to know and understand how the system is reacting to your commands.
While this procedure works well, you do not have control of all the functions you call. You can ensure you always pass by value by passing a statement, not a variable. For example, the calling program could be rewritten as
* // irrelevant things happened above
myVar = "Sprezzatura"
invList = getSomething( myVar : "" )
* // do more stuff
param1 in getSomething will still be passed "Sprezzatura", but the result will be created from the passed in statement myVar:"" which is resolved before being passed into the function.
While passing by reference and passing by value may be generally inconsequential in Basic+ programming, it's an important concept that must be understood. Failure to understand could have adverse effects on your systems, as a future post shall show.
Labels: behaviour, Compiler, variables
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home