need help with type mismatch error

New to FreeBASIC? Post your questions here.
Post Reply
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

need help with type mismatch error

Post by StillLearning »

I have attempted to compile the following code:

Code: Select all

declare sub ValidateLeadingSign(Digit as string,PlusExists as Byte, _
                                MinusExists as Byte)
Dim shared as string TxtLine,TxtChar
Dim shared as short  TxtPos


'Validate unairy + or - sign for number
sub ValidateLeadingSign( _
  Byref Digit as string, _
  Byref PlusExists as byte, _
  Byref MinusExists as byte)

'Make sure PlusExists is defined with "Byref" so this value can be passed back.
'Make sure MinusExists is defined with "Byref" so this value can be passed back.
Dim TxtVal as Ubyte
do
  TxtChar= MID(TxtLine,TxtPos,1)
  TxtPos = TxtPos + 1
  TxtVal = asc(TxtChar)
  If TxtVal = 43 then   
    'check for "+"
    PlusExists = not PlusExists
    if PlusExists = 0 then
      Print "Error cannot have more than 1 leading + sign in a number"
      exit do
    end if
    if MinusExists then
      print "error bad signed number"
      exit do
    end if
  elseif TxtVal = 45 then
    'check for "-"
    MinusExists = not MinusExists
    if MinusExists = 0 then
      Print "Error cannot have more than 1 leading - sign in a number"
      exit do
    end if
    if PlusExists then
      print "error bad signed number"
      exit do
    end if
  else
    exit do
  end if
  Digit = Digit + TxtChar
loop
end sub

Dim Digit as string
Dim MinusExists as Byte
Dim PlusExists as Byte

TxtPos = 1
MinusExists = 0
PlusExists = 0

input "enter floating-point number ";TxtLine
TxtLine = TxtLine +";"+chr$(13)

Digit = ""
ValidateLeadingSign(Digit,PlusExists,MinusExists)
    If (PlusExists + MinusExists) <> 0 then
      Print "error a floating point hex number MUST be unsigned."
    else
      print "no error" ;Digit 
    end if
end
When I compile it I get the following error:

Code: Select all

      exptest.bas<12> error 57: type mismatch, at parameter 2 <PlusExists> of ValidateLeadingSign<> in 'byref MinusExists as byte>
 
I do not see the mismatch for the variable PlusExists. Could someone please explain what I am doing wrong.
fxm
Moderator
Posts: 12109
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: need help with type mismatch error

Post by fxm »

The declarations of parameter types and passing modes must be matching between procedure declaration line and procedure definition line. By default, a String is passed by reference while a Byte is passed by value. It is why the compiler only rizes an error on PlusExists and MinusExists:

Code: Select all

declare sub ValidateLeadingSign(Byref Digit as string,Byref PlusExists as Byte, _
                                Byref MinusExists as Byte)
The string type suffix "$" is obligatory in the -lang qb dialect, optional in the -lang fblite dialect, but forbidden in the -lang fb dialect:

Code: Select all

TxtLine = TxtLine +";"+chr(13)
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

Re: need help with type mismatch error

Post by StillLearning »

Thank you for your help.

So if strings are passed by reference and a byte is passed by value why do I have to say "byref" and "byval" when the compiler knows that information? It would be much easier for the programmer not to have to do that because, as you can see, it is easy to get it wrong.
fxm
Moderator
Posts: 12109
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: need help with type mismatch error

Post by fxm »

Inside your procedure 'ValidateLeadingSign()', you want to modify in the global scope the values of the parameters 'Digit', 'PlusExists' and 'MinusExists'. So these three must be passed by reference (pass only a copy is wrong).
As by default a String is passed by reference and a Byte is passed by value, only the two Byte parameters must be explicitly declared passed by reference (do it for the String is optional).

Minimum declaration:

Code: Select all

declare sub ValidateLeadingSign(Digit as string,Byref PlusExists as Byte, _
                                Byref MinusExists as Byte)

Code: Select all

sub ValidateLeadingSign( _
  Digit as string, _
  Byref PlusExists as byte, _
  Byref MinusExists as byte)
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

Re: need help with type mismatch error

Post by StillLearning »

So if I understand correctly, a variable that is not a string, passed to a procedure is "byref" unless you are modifying its value within the procedure then it is passed "byval"?
fxm
Moderator
Posts: 12109
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: need help with type mismatch error

Post by fxm »

Modifying or not a parameter inside a procedure does not modify the passing mode by default:
  • In '-lang fb dialect', 'ByVal' is the default parameter passing convention for all built-in types except 'String' and 'User-Defined Type' which are passed 'ByRef' by default. The 'ZString' and 'WString' built-in types are also passed 'ByRef' by default, but passing 'ByVal' is forbidden. Arrays are always passed 'ByRef' and the use of the specifier 'ByRef' or 'ByVal' is forbidden.
  • In '-lang qb' and '-lang fblite' dialects, 'ByRef' is the default parameter passing convention.
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

Re: need help with type mismatch error

Post by StillLearning »

Ok, I understand it better now. Thank you for the info.
Post Reply