byVal, byRef
byVal and byRef are keywords in the declaration of Meths .
byVal
The following parameter is to be passed to the method as a copy (Default). „byVal“ is therefore obsolete, because parameters are passed as a copy by default. Copy meaning the the source variable is left untouched.
byRef
The following parameter is to be passed to the method by reference. Reference meaning that a address pointer to the source object or variable is passed as a parameter and the method can modify the source variable. Expressions can't be passed to the method because they are neither objects, variables or data structures.
Recurrent Declarationen in Classes
When using Classes, you may declare items with the same name again in or out of a Structure because they occupy separate namespaces. However, if you reference a method within a class with byRef, the compiler will not be able to distinguish the name structure of the object and will report a collision. In such a case use the Structur declaration of a referenced structure in the main program only.
Pass a Parameter as a Copy
Example 1:
' Main program dim myvalue as integer dim s as string Display("My Number: ",33) ' Call Subroutine ' Alternative call Display = 12345 s = "Different Number: " call Display(s,myvalue) do loop ' Subroutine procedure Display(text as string, a as byte) dim x as integer ' local variable x=a*100 print text+str(x) endproc
Pass a Parameter by Reference
Example 1: Variables
dim a as byte a=23 test(a) print "a = ";str(a) ' Output: a = 42 do loop procedure test(byRef c as byte) print "c = ";str(c) ' Output: c = 23 c = 42 endproc
Example 2: Object Structure
struct point byte x byte y endstruct dim p as point p.x=23 test(p) print "p.x = ";str(p.x) ' Output: p.x = 42 do loop procedure test(byRef c as point) print "c.x = ";str(c.x) ' Output: c.x = 23 c.x = 42 endproc
Beispiel 3: Constant Object
test(text) ' Output: c.PString(0) = "hallo" test("ballo") ' Output: c.PString(0) = "ballo" do loop procedure test(byRef c as data) print "c.PString(0) = ";34;c.PString(0);34 endproc data text .db 5,"hallo" enddata
Example 4: Memory Object
dim a(127),i as byte for i=0 to a.Ubound a(i)=i next test(a()) do loop ' Direct access to external array procedure test(byRef c as sram) dim i as byte for i=0 to 63 print str(c.ByteValue(i)) next endproc