Can someone explain the usage of VAR to me?

BlitzMax Forums/BlitzMax Programming/Can someone explain the usage of VAR to me?

Russell(Posted 2005) [#1]
In the manual we have two demonstrations of the Var keyword:
Function ReturnMultiplevalues(a Var,b Var,c Var)
	a=10
	b=20
	c=30
	Return
End Function

Local x,y,z

ReturnMultipleValues(x,y,z)

Print "x="+x	'10
Print "y="+y	'20
Print "z="+z	'30

and
Local c[]=[1,2,3,4]
Local p:Int Ptr

p=c
Print "var p="+Var p	'1

p:+1
Print "var p="+Var p	'2

p:+1
Print "var p="+Var p	'3

p:+1
Print "var p="+Var p	'4

Firstly, why does Var come after the variable in the first example and before the variable in the second example?

In the first example, it looks like the variables are being passed "By Reference" so that the actual variables passed are affected by the function call.

In the second example, 'p' is pointing to the first element of the array 'c' and adding 1 to the pointer simply jumps to the next entry (which is one integer 'further up' in this case).

I basically get it, but just wonder if there's some significance as to why Var is not consistantly used in the two examples.

Thanks,
Russell


ImaginaryHuman(Posted 2005) [#2]
I'm not sure what the difference is between before and after. But it's my understanding that Var lets you refer/ pass a specific variable to a function rather than copy it's value to another.

So with normal function parameters, you would pass some values to the function and it would then create new variables to put those values in. Those new variables would then be available inside the function as local. Modifying them wouldn't modify the original variables, since the data is a copy.

If you use Var when specifying the parameters of the function, you're defining that these parameters should a `reference to` real variables that you want to use. It creates a pointer to that variable. So instead of copying the value of the variable into anther local one inside the function, it actually lets you modify the original variable itself. The temporary local variable name is just an `alias` for that real variable. So changing the value of that local variable actually changes the content of the real original variable outside the function.

It's kind of like a per-variable way of making a variable temporarily Global for the purposes of using in a specific function, while not being Global to anywhere else unless also defined as such.

Function myfunct(b:Int Var)
b:+1
End Function
Local a:Int=2
myfunct(a)
Print a

should print 3. The `b` variable, local to the function, isn't so much a NEW variable as it is a reference to the actual `a` variable. Manipulating the b variable actually manipulates the a variable, as if they are one variable. So the b variable, used with `Var`, is sort of a pointer - in this case an int pointer to `a`. The only thing is, to the programmer, you don't use it as an int pointer (cus that would mean you'd have to add [0] on the end of it to change the value - blitzmax does that part for you).

So in the second example you gave, notice that p is an int ptr. They are using Var to refer to, I guess, printing the content of the variable that p points to. In this case it points to the c[] array. So printing Var p actually prints c[0], and then c[1] and so on.

I guess you are allowed to put the Var before or after the pointer name.


Robert(Posted 2005) [#3]
I really think that calling it 'Ref' might be easier, since it makes it obvious that you are passing a 'reference' to the function, rather than a value.


Kanati(Posted 2005) [#4]
mark just likes to make things... different. :)


Dreamora(Posted 2005) [#5]
VAR is not different ... just a short form of what it originally seems to come from: ByVar from Pascal / Modulo / Oberon and other languages that addapted it.


ImaginaryHuman(Posted 2005) [#6]
I just thought it means `variable`


Russell(Posted 2005) [#7]
@AngelDaniel:
Thanks for the explanation A.D., but in my post you'll see that I already knew that... (I just was mainly wondering why Var came before in some instances, and after in other instances).

"I guess you are allowed to put the Var before or after the pointer name."
Actually, I thought this, too (and that would be fine with me), but you will get a sytax error if you reverse the order in either of the two code examples above.

This type of parameter passing is sometimes called 'By Reference' (ByRef) or 'By Address' (which is what it really is).

Anyway, the two uses of Var are not actually doing the same thing:
The first one is saying 'use the address of the passed variable...'
And the second example is saying 'Use the value *AT* the address (p is declared as a pointer, remember).

Maybe two different keywords should have been used, instead of placing the keyword before or after for the different uses?

Russell

@A.D.
"I just thought it means `variable"
I think it stands for VARy hard to understand ;)