Syntax error, byval

Bahamit

New Member
Hello all, I'd like to start by saying this is a great program that has potential to change the world :sign0098:

Anyways, I've been working on trying to get a code working for ~2 hours now and no hope. The original (VB-verified and working) code is:
B4X:
    Function gcd(ByVal a As Long, ByVal b As Long)
        Dim temp As Long
        Do While b > 0
            temp = b
            b = a Mod b
            a = temp
        Loop

        gcd = a
    End Function

and the one I have in B4A is:
B4X:
Sub gcd (Byval a As Long, Byval b As Long) As Long
 
        Dim temp As Long
        Do While b > 0
            temp = b
            b = a Mod b
            a = temp
        Loop

        gcd = a
    End Sub

Whenever I try to compile and run my program I get a syntax error on the top line,
Sub gcd (Byval a As Long, Byval b As Long) As Long.
I can't seem to get this working. In the end it's supposed to reduce my fractions.. Any tips?
 

Bahamit

New Member
Thanks, I saw those earlier... However I don't understand arrays and believe I found what's to be learned on the VB>VB4A page. Anyways, I've made some (progress?) to my original code and this is where I'm at now:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim b As Long
Dim a As Long
b = Denom.Text
a = Num.text
End Sub
Sub gcd 
 
        Dim temp As Long
        Do While b > 0
            temp = b
            b = a Mod b
            a = temp
        Loop

        gcd = a
    End Sub

I have removed the error previously found and come up with a new one,
Compiling code. Error
Error compiling program.
Error description: Array expected.
Occurred on line: 46
cfac = gcd(num1, deno)
Word: (
.
If this can't work I'm going to look for a solution to convert a decimal to fraction. Thoughts?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Reading between the two subs that you have posted, it appears that what you re looking for is something like:

B4X:
Sub gcd(a As Long,b As Long) as Long
 
    Dim temp As Long
    Do While b > 0
        temp = b
        b = a Mod b
        a = temp
    Loop

    Return a
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…