Need help with passing a parameter

New to FreeBASIC? Post your questions here.
StillLearning
Posts: 33
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: 33
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: 1265
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: 9927
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
SamL
Posts: 25
Joined: Nov 23, 2019 17:30

Re: Need help with passing a parameter

Postby SamL » Jul 11, 2020 18:13

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


yes!! by using a type. here are some ways to do that!



Code: Select all


type test
   
    as long number_1
    as long number_2
   
end type

function example1 (param as long ) as test
   
    dim my_udt as test
   
    my_udt.number_1 = param
    my_udt.number_2 = param*2
   
    return my_udt
   
end function

' you can do this
print "example 1"
print example1( 5 ).number_1
print example1( 5 ).number_2


'or you can do this
print "example 2"
dim my_type as test

my_type = example1(10)
print my_type.number_1
print my_type.number_2

'or you can make an array of the type.
print "example 3"
dim my_type_2(1 to 2) as test

my_type_2(1) = example1(10)
my_type_2(2) = example1(3)

print my_type_2(1).number_1
print my_type_2(1).number_2
print my_type_2(2).number_1
print my_type_2(2).number_2

sleep



Also you can send a type into a function and return it like so.

Code: Select all


type test
   
    as long number_1
    as long number_2
   
end type

function example2( my_udt as test ) as test
    my_udt.number_2 = my_udt.number_1*2
   
    return my_udt
end function


print "example 5"
dim my_type_3 as test
my_type_3.number_1 = 5

my_type_3 = example2(my_type_3)

print my_type_3.number_2
'or
print "example 6"

print example2(Type(8,0)).number_2

sleep




hope this helps!!

-SamL

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 4 guests