PDA

View Full Version : byte example + udt



Lionheart008
30-01-2010, 11:41
good morning,

possible to check this "byte" code I have made ?
perhaps I am not awaken, it's possible to get a result for these two functions ?
in my cases it's both "0", that's ok ? I thought it should be "1" ?

a)
PrintL "function 2 does not work? " + byteLocal(99)
b)
PrintL "function 1 does not work? " + SendByte(77)

may be I have done something wrong, don't know.


' Empty GUI script created on 01-30-2010 09:48:04 by (ThinAIR)
'-------------------- test bytes by franko ------------------------------

Uses "console"

' Simple assignment ---------------------------------
Dim b,c As Byte
Dim by? As Byte

b = 255 : PrintL "-> 255: " + b
by? = 101 : PrintL "-> by?: " + by?
PrintL

' Math -----------------------------

b = b + 10 : PrintL "-> b+10: " + b
c = b * 20 : PrintL "-> b*20: " + c
b = 0*81
PrintL "-> 0*81: " + b ' Positive 129
b = -127
PrintL "-> b = -127: " + b
PrintL

' UDT Assignment -----------------------------

Type ByteType
x As Byte
y As Byte
End Type

Dim bt As ByteType

bt.x = 3
bt.y = 8

PrintL "bt.x: " + bt.x
PrintL "bt.y: " + bt.y
PrintL

' Copy UDT ---------------------------------

Dim btCopy As ByteType

btCopy = bt

PrintL "btCopy.x: " + btCopy.x
PrintL "btCopy.y: " + btCopy.y
PrintL

' Array Assignment ---------------------------

Dim ByteArray(4) As Byte

ByteArray(1) = b
ByteArray(2) = 2
ByteArray(3) = 3
ByteArray(4) = 4

PrintL "ByteArray(1): " + ByteArray(1)
PrintL "ByteArray(2): " + ByteArray(2)
PrintL "ByteArray(3): " + ByteArray(3)
PrintL "ByteArray(4): " + ByteArray(4)
PrintL

' Copy Array ------------------------------------
Dim ByteArrayCopy(4) As Byte
ByteArrayCopy(1) = ByteArray(1)
ByteArrayCopy(2) = ByteArray(2)
ByteArrayCopy(3) = ByteArray(3)
ByteArrayCopy(4) = ByteArray(4)

printl ByteArrayCopy(1)
printl ByteArrayCopy(2)
printl ByteArrayCopy(3)
printl ByteArrayCopy(4)
PrintL

' Function: Return a byte ------------------------

Function ReturnByte() As Byte
Local result As Long
Result = 42
Function = result
End Function

PrintL "returnByte: " + ReturnByte()
PrintL

' Subroutine: Send a byte -------------------------

Sub SendByte(ByVal ch As Byte)
ch = ch + 1
PrintL "ch + 1: " + ch
End Sub

PrintL "function 1 does not work? " + SendByte(77)
PrintL

' Function: Local variable ------------------------

Function ByteLocal(ByVal ch As Byte) As Byte
Dim chwork As Byte
Local result As Long
chwork = ch + 1
PrintL "chwork = ch + 1 " + chwork
Result = -1
End Function

PrintL "function 2 does not work? " + byteLocal(99)
PrintL

WaitKey

nice winterday, frank

Michael Clease
30-01-2010, 12:15
I will give you a hint Subroutines do not return values, only FUNCTIONS.

Michael Hartlef
30-01-2010, 12:47
Another hint, you don't return your result in function 2.

Petr Schreiber
30-01-2010, 13:12
Guys already decrypted the roots of problems, I have just one suggestion - please do not use "?" in your variable names. It seems to be currently tolerated, but I would recommend to not use it and just stick to A-Z, 0-9 and _ in the names, as help file says:


Cannot contain embedded period, space and, in general, punctuation



Thanks,
Petr

Lionheart008
30-01-2010, 13:40
thanks! :)

I have fixed little problems here, because I have forgotten one line in function 2. sorry.


' Empty GUI script created on 01-30-2010 09:48:04 by (ThinAIR)
'-------------------- test bytes by franko ------------------------------

Uses "console"

' Simple assignment
Dim b,c As Byte
Dim by As Byte '-- works with by? too ;)

b = 255 : PrintL "-> 255: " + b
by = 101 : PrintL "-> by: " + by
PrintL

' Math -----------------------------

b = b + 10 : PrintL "-> b+10: " + b
c = b * 20 : PrintL "-> b*20: " + c
b = 0*81
PrintL "-> 0*81: " + b ' Positive 129
b = -127
PrintL "-> b = -127: " + b
PrintL

' UDT Assignment -----------------------------

Type ByteType
x As Byte
y As Byte
End Type

Dim bt As ByteType

bt.x = 3
bt.y = 8

PrintL "bt.x: " + bt.x
PrintL "bt.y: " + bt.y
PrintL

' Copy UDT ---------------------------------

Dim btCopy As ByteType

btCopy = bt

PrintL "btCopy.x: " + btCopy.x
PrintL "btCopy.y: " + btCopy.y
PrintL

' Array Assignment ---------------------------

Dim ByteArray(4) As Byte

ByteArray(1) = b
ByteArray(2) = 2
ByteArray(3) = 3
ByteArray(4) = 4

PrintL "ByteArray(1): " + ByteArray(1)
PrintL "ByteArray(2): " + ByteArray(2)
PrintL "ByteArray(3): " + ByteArray(3)
PrintL "ByteArray(4): " + ByteArray(4)
PrintL

' Copy Array ------------------------------------ ?
Dim ByteArrayCopy(4) As Byte
ByteArrayCopy(1) = ByteArray(1)
ByteArrayCopy(2) = ByteArray(2)
ByteArrayCopy(3) = ByteArray(3)
ByteArrayCopy(4) = ByteArray(4)

printl ByteArrayCopy(1)
printl ByteArrayCopy(2)
printl ByteArrayCopy(3)
printl ByteArrayCopy(4)
PrintL

' Function: Return a byte ------------------------

Function ReturnByte() As Byte
Local result As Long
Result = 42
Function = result
End Function

PrintL "returnByte: " + ReturnByte()
PrintL

' Subroutine: Send a byte -------------------------

Sub SendByte(ByVal ch As Byte)
ch = ch + 1
PrintL "test: ch + 1: " + ch
End Sub

PrintL

' Function: Local variable ------------------------

Function ByteLocal(ByVal ch As Byte) As Byte
Dim chwork As Byte
Local result As Long
chwork = ch + 1
result = -1 + chwork
PrintL "chwork = ch + 1: " + chwork
Function = result
End Function

PrintL "function 2 does work: " + byteLocal(99)
PrintL

WaitKey

thanks for the hints boys! my subroutine was at first a function, then I have changed it into a sub for testing purpose, so it was my mistake. michael c., all ok here now.

the "?" in variable names I have only tested here to see what happens ;)

best regards, nice week-end, frank