Return multiple values: Difference between revisions

m
→‎{{header|FutureBasic}}: Remove the unsupported 'ConsoleWindow'
m (→‎{{header|FutureBasic}}: Remove the unsupported 'ConsoleWindow')
Line 1,224:
Here is an example of returning multiple values using pointers:
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"
 
local fn ReturnMultipleValues( strIn as Str255, strOut as ^Str255, letterCount as ^long )
dim as Str255 s
 
// Test if incoming string is empty, and exit function if it is
if strIn[0] == 0 then exit fn
 
// Prepend this string to incoming string and return it
s = "Here is your original string: "
strOut.nil$ = s + strIn
 
// Get length of combined string and return it
// Note: In FutureBasic string[0] is interchangeable with Len(string)
letterCount.nil& = strIn[0] + s[0]
end fn
 
dim as Str255 outStr
dim as long outCount
 
fn ReturnMultipleValues( "Hello, World!", @outStr, @outCount )
print outStr; ". The combined strings have "; outCount; " letters in them."
 
HandleEvents
</syntaxhighlight>
 
Line 1,255:
Another way to pass multiple values from a function is with records (AKA structures):
<syntaxhighlight lang="text">
include "ConsoleWindow"
 
// Elements in global array
_maxDim = 3
 
begin record Addresses
dim as Str63 name
dim as Str15 phone
dim as long zip
end record
 
begin globals
dim as Addresses gAddressData(_maxDim)
end globals
 
local fn FillRecord( array(_maxDim) as Addresses )
array.name(0) = "John Doe"
array.name(1) = "Mary Jones"
array.name(2) = "Bill Smith"
 
array.phone(0) = "555-359-4411"
array.phone(1) = "555-111-2211"
array.phone(2) = "555-769-8071"
 
array.zip(0) = 12543
array.zip(1) = 67891
array.zip(2) = 54321
end fn
 
Line 1,287 ⟶ 1,285:
fn FillRecord( gAddressData(0) )
 
dim as short i
 
for i = 0 to 2
print gAddressData.name(i); ", ";
print gAddressData.phone(i); ", Zip:";
print gAddressData.zip(i)
next
 
HandleEvents
</syntaxhighlight>
 
Line 1,305 ⟶ 1,304:
You can also use global arrays to return multiple values from a function as in this example:
<syntaxhighlight lang="text">
include "ConsoleWindow"
 
// Elements in global array
_maxDim = 3
 
begin globals
dim as Str31 gAddressArray(_maxDim, _maxDim)
end globals
 
Line 1,317 ⟶ 1,314:
array( 0, 0 ) = "John Doe"
array( 1, 0 ) = "Mary Jones"
array( 2, 0 ) = "Bill Smith"
 
array( 0, 1 ) = "555-359-4411"
Line 1,331 ⟶ 1,328:
fn FillRecord( gAddressArray( 0, 0 ) )
 
dim as short i, j
 
for i = 0 to 2
j = 0
print gAddressArray(i, j ); ", ";
print gAddressArray(i, j + 1); ", Zip: ";
print gAddressArray(i, j + 1)
next
 
HandleEvents
</syntaxhighlight>
 
Line 1,350 ⟶ 1,349:
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">
include "ConsoleWindow"
 
begin globals
// An FB container can hold up to 2GB of data, contingent on system memory
dim as container gC1, gC2
end globals
 
Line 1,396 ⟶ 1,393:
// Check the new results
print gC1 : print gC2
 
HandleEvents
</syntaxhighlight>
 
408

edits