Need help with passing a parameter

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

Need help with passing a parameter

Postby StillLearning » Mar 20, 2020 2:47

Enclosed is a sample of a subroutine that I want to pass back a byte value. Here is a copy of the way it is currently coded:

Code: Select all

declare sub ValidateLeadingSign(Digit as string,MinusExists as Byte)
dim shared as string TxtLine

'Validate unairy + or - sign for number
sub ValidateLeadingSign(_
  Digit as string,_
  Byval MinusExists as Byte)

Digit = "no negative"
if TxtLine = "-" then
  print "before MinusExists = ";MinusExists
  MinusExists = not MinusExists
  print "after MinusExists = ";MinusExists
  Digit = "Found negative"
end if
end sub

Dim Digit as string
Dim MinusExists as Byte

MinusExists = 1

TxtLine = "-"
ValidateLeadingSign(Digit,MinusExists)
    Print "value of MinusExists =";MinusExists
    If MinusExists then
      Print "error a floating point hex number MUST be unsigned."
    end if
    print Digit
end


The way the code is currently written MinusExists changes its value in the subroutine but never passes it back.
I tried to do it by changing the "byval" to "byref" but get a type mismatch error. How do I change my code to pass back
MinusExists with its value changed? The only other way I got it to work was to have MinusExists be a shared variable.
andykmv
Posts: 52
Joined: Feb 12, 2015 9:50

Re: Need help with passing a parameter

Postby andykmv » Mar 20, 2020 3:12

Subs are procedures that don't return values. need to use functions

some extra help starting here:
https://www.freebasic.net/wiki/wikka.php?wakka=ProPgProcedures
StillLearning
Posts: 23
Joined: Aug 27, 2019 22:22

Re: Need help with passing a parameter

Postby StillLearning » Mar 20, 2020 4:07

Can a Function return more than one value? If so how do you do it?
paul doe
Posts: 1139
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Need help with passing a parameter

Postby paul doe » Mar 20, 2020 5:07

StillLearning wrote:Can a Function return more than one value? If so how do you do it?

No, but you can return a type:

Code: Select all

type _
  ValidatedSign
 
  declare constructor( _
    byref as const string, _
    byval as byte )
 
  as string _
    digit
  as byte _
    minusExists
end type

constructor _
  ValidatedSign( _
    byref aDigit as const string, _
    byval aMinusExists as byte )
 
  digit => aDigit
  minusExists => aMinusExists
end constructor

'Validate unairy + or - sign for number
function ValidateLeadingSign( _
  byref txtLine as string, _
  byval minusExists as byte ) _
  as ValidatedSign
 
  dim as string _
    digit = "no negative"
 
  if txtLine = "-" then
    print "before MinusExists = ";minusExists
    minusExists = not minusExists
    print "after MinusExists = ";minusExists
    digit = "Found negative"
  end if
 
  return( ValidatedSign( digit, minusExists ) )
end function

dim as string _
  TxtLine = "-"
dim as byte _
  MinusExists => 1

var _
  result => ValidateLeadingSign( TxtLine, MinusExists )
 
Print "value of MinusExists =";MinusExists

If result.MinusExists then
  Print "error a floating point hex number MUST be unsigned."
end if

print result.Digit

sleep()
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Need help with passing a parameter

Postby fxm » Mar 20, 2020 5:54

StillLearning wrote:The way the code is currently written MinusExists changes its value in the subroutine but never passes it back.
I tried to do it by changing the "byval" to "byref" but get a type mismatch error.

To answer your initial question, this above is the right solution, but you must do the change both in the declaration and in the definition of the subroutine:

Code: Select all

declare sub ValidateLeadingSign(Digit as string,byref MinusExists as Byte)
dim shared as string TxtLine

'Validate unairy + or - sign for number
sub ValidateLeadingSign(_
  Digit as string,_
  byref MinusExists as Byte)

Digit = "no negative"
if TxtLine = "-" then
  print "before MinusExists = ";MinusExists
  MinusExists = not MinusExists
  print "after MinusExists = ";MinusExists
  Digit = "Found negative"
end if
end sub

Dim Digit as string
Dim MinusExists as Byte

MinusExists = 1

TxtLine = "-"
ValidateLeadingSign(Digit,MinusExists)
    Print "value of MinusExists =";MinusExists
    If MinusExists then
      Print "error a floating point hex number MUST be unsigned."
    end if
    print Digit
end

Return to “Beginners”

Who is online

Users browsing this forum: MSN [Bot] and 2 guests