Return multiple values: Difference between revisions

Content added Content deleted
m (→‎{{header|FutureBasic}}: Remove the unsupported 'ConsoleWindow')
Line 1,224: Line 1,224:
Here is an example of returning multiple values using pointers:
Here is an example of returning multiple values using pointers:
<syntaxhighlight lang="futurebasic">
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"

local fn ReturnMultipleValues( strIn as Str255, strOut as ^Str255, letterCount as ^long )
local fn ReturnMultipleValues( strIn as Str255, strOut as ^Str255, letterCount as ^long )
dim as Str255 s
Str255 s

// Test if incoming string is empty, and exit function if it is
// Test if incoming string is empty, and exit function if it is
if strIn[0] == 0 then exit fn
if strIn[0] == 0 then exit fn

// Prepend this string to incoming string and return it
// Prepend this string to incoming string and return it
s = "Here is your original string: "
s = "Here is your original string: "
strOut.nil$ = s + strIn
strOut.nil$ = s + strIn

// Get length of combined string and return it
// Get length of combined string and return it
// Note: In FutureBasic string[0] is interchangeable with Len(string)
// Note: In FutureBasic string[0] is interchangeable with Len(string)
letterCount.nil& = strIn[0] + s[0]
letterCount.nil& = strIn[0] + s[0]
end fn
end fn


dim as Str255 outStr
Str255 outStr
dim as long outCount
long outCount


fn ReturnMultipleValues( "Hello, World!", @outStr, @outCount )
fn ReturnMultipleValues( "Hello, World!", @outStr, @outCount )
print outStr; ". The combined strings have"; outCount; " letters in them."
print outStr; ". The combined strings have ";outCount; " letters in them."

HandleEvents
</syntaxhighlight>
</syntaxhighlight>


Line 1,255: Line 1,255:
Another way to pass multiple values from a function is with records (AKA structures):
Another way to pass multiple values from a function is with records (AKA structures):
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
include "ConsoleWindow"

// Elements in global array
// Elements in global array
_maxDim = 3
_maxDim = 3


begin record Addresses
begin record Addresses
dim as Str63 name
Str63 name
dim as Str15 phone
Str15 phone
dim as long zip
long zip
end record
end record


begin globals
begin globals
dim as Addresses gAddressData(_maxDim)
Addresses gAddressData(_maxDim)
end globals
end globals


local fn FillRecord( array(_maxDim) as Addresses )
local fn FillRecord( array(_maxDim) as Addresses )
array.name(0) = "John Doe"
array.name(0) = "John Doe"
array.name(1) = "Mary Jones"
array.name(1) = "Mary Jones"
array.name(2) = "Bill Smith
array.name(2) = "Bill Smith"

array.phone(0) = "555-359-4411"
array.phone(0) = "555-359-4411"
array.phone(1) = "555-111-2211"
array.phone(1) = "555-111-2211"
array.phone(2) = "555-769-8071"
array.phone(2) = "555-769-8071"

array.zip(0) = 12543
array.zip(0) = 12543
array.zip(1) = 67891
array.zip(1) = 67891
array.zip(2) = 54321
array.zip(2) = 54321
end fn
end fn


Line 1,287: Line 1,285:
fn FillRecord( gAddressData(0) )
fn FillRecord( gAddressData(0) )


dim as short i
short i

for i = 0 to 2
for i = 0 to 2
print gAddressData.name(i); ", ";
print gAddressData.name(i); ", ";
print gAddressData.phone(i); ", Zip:";
print gAddressData.phone(i); ", Zip:";
print gAddressData.zip(i)
print gAddressData.zip(i)
next
next

HandleEvents
</syntaxhighlight>
</syntaxhighlight>


Line 1,305: Line 1,304:
You can also use global arrays to return multiple values from a function as in this example:
You can also use global arrays to return multiple values from a function as in this example:
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
include "ConsoleWindow"

// Elements in global array
// Elements in global array
_maxDim = 3
_maxDim = 3


begin globals
begin globals
dim as Str31 gAddressArray(_maxDim, _maxDim)
Str31 gAddressArray(_maxDim, _maxDim)
end globals
end globals


Line 1,317: Line 1,314:
array( 0, 0 ) = "John Doe"
array( 0, 0 ) = "John Doe"
array( 1, 0 ) = "Mary Jones"
array( 1, 0 ) = "Mary Jones"
array( 2, 0 ) = "Bill Smith
array( 2, 0 ) = "Bill Smith"


array( 0, 1 ) = "555-359-4411"
array( 0, 1 ) = "555-359-4411"
Line 1,331: Line 1,328:
fn FillRecord( gAddressArray( 0, 0 ) )
fn FillRecord( gAddressArray( 0, 0 ) )


dim as short i, j
short i, j


for i = 0 to 2
for i = 0 to 2
j = 0
j = 0
print gAddressArray(i, j ); ", ";
print gAddressArray(i, j ); ", ";
print gAddressArray(i, j + 1); ", Zip: ";
print gAddressArray(i, j + 1); ", Zip: ";
print gAddressArray(i, j + 1)
print gAddressArray(i, j + 1)
next
next

HandleEvents
</syntaxhighlight>
</syntaxhighlight>


Line 1,350: Line 1,349:
Here is another example using FB's containers -- bit buckets that can hold up to 2GB of data contingent on system memory.
Here is another example using FB's containers -- bit buckets that can hold up to 2GB of data contingent on system memory.
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
include "ConsoleWindow"

begin globals
begin globals
// An FB container can hold up to 2GB of data, contingent on system memory
// An FB container can hold up to 2GB of data, contingent on system memory
dim as container gC1, gC2
container gC1, gC2
end globals
end globals


Line 1,396: Line 1,393:
// Check the new results
// Check the new results
print gC1 : print gC2
print gC1 : print gC2

HandleEvents
</syntaxhighlight>
</syntaxhighlight>