[SOLVED] How to get multiple values returned from a function?

New to FreeBASIC? Post your questions here.
Post Reply
Laurens
Posts: 17
Joined: Mar 16, 2022 9:16
Location: Flevoland, the Netherlands

[SOLVED] How to get multiple values returned from a function?

Post by Laurens »

At this moment I'm struggling with conversion of an old GWBasic program, the GOSUB command needs to be replaced by a function, or may be you got a better idea about it. The main goal is to provide the function with some variables, the function does its work, and I got some data in return. The code looks like this:

Code: Select all

Declare Function GPrint (StartX As Integer, StartY As Integer, FontSize As Integer, EndX As Integer, EndY As Integer, Txt As String)

Function  GPrint (StartX As Integer, StartY As Integer, FontSize As Integer, EndX As Integer, EndY As Integer, Txt As String)
	'Calculating where to put the fonts and placing them on the screen

	Return EndX
	Return EndY
End Function
When I'm running the application, the function returns zero. Any help would be appreciated.
Last edited by Laurens on Mar 24, 2022 3:45, edited 1 time in total.
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: How to get multiple values returned from a function?

Post by Vortex »

Hello Laurens,
How to get multiple values returned from a function?
To return multiple values from a function, you could fill a structure ( Used Defined Type ) and return a pointer to this UDT.
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to get multiple values returned from a function?

Post by SARG »

You can use byref for the parameters : byref EndX As Integer, byref EndY As Integer.
In this case they are directly updated in the function.

Another solution could be to use a structure / user defined type (udt) with 2 fields x and y.

Edit : Vortex faster ;-)
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to get multiple values returned from a function?

Post by badidea »

Both methods in code:

Code: Select all

'--- Method1: Via sub & byref parameter ---

Sub GPrintSub (StartX As Integer, StartY As Integer, FontSize As Integer, _
	Byref EndX As Integer, Byref EndY As Integer, Txt As String)
	'Calculating where to put the fonts and placing them on the screen
	EndX = 3
	EndY = 4
End Sub

Dim As Integer X = 1, Y = 2
Print X, Y
GPrintSub (0, 0, 0, X, Y, "text")
Print X, Y
Print

'--- Method2: Via function return & UDT ---

Type Int2d
	Dim As Integer x, y
End Type

Function GPrintFunc (StartX As Integer, StartY As Integer, FontSize As Integer, _
	EndX As Integer, EndY As Integer, Txt As String) as Int2d
	'Calculating where to put the fonts and placing them on the screen
	EndX = 3
	EndY = 4
	Return Type(EndX, EndY)
End Function

Dim As Int2d V = Type(1, 2)
Print V.X, V.Y
V = GPrintFunc (0, 0, 0, V.X, V.Y, "text")
Print V.X, V.Y
Print
Laurens
Posts: 17
Joined: Mar 16, 2022 9:16
Location: Flevoland, the Netherlands

Re: How to get multiple values returned from a function?

Post by Laurens »

Thank you all for your solutions. I started with the shortest one, the ByRef version, and it works perfectly.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: [SOLVED] How to get multiple values returned from a function?

Post by coderJeff »

In the past when I have had to convert old QB code, my approach has always been to isolate the GOSUB calls and pull the code out in to a function.

BYREF makes the most sense to me if there are multiple variables and they need to change as a result of the call.

Just for fun, a not very efficient use of some (probably) little known syntax utilizing the LET keyword:

Code: Select all

type Double3Tuple
	as double signval
	as double absolute
	as double squared 
end type

function Numericate( byval n as double ) as Double3Tuple
	return type( sgn(n), abs(n), n * n )
end function 

dim as double a, b, c, n

n = -45.6 
let( a, b, c ) = Numericate( n )

print "n = " & n
print "a = " & a
print "b = " & b
print "c = " & c

sleep
It can be considered inefficient because the data is passed and returned by value resulting in extra copies of the data in temporary memory locations.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: [SOLVED] How to get multiple values returned from a function?

Post by srvaldez »

hi coderJeff, interesting example
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [SOLVED] How to get multiple values returned from a function?

Post by fxm »

Operator LET() (Assignment) page in documentation.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: [SOLVED] How to get multiple values returned from a function?

Post by srvaldez »

thank you fxm for the excellent help file, it's a veritable goldmine :D
Post Reply