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">
local fn ReturnMultipleValues( strIn as Str255, strOut as ^Str255, letterCount as ^long )
// 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
fn ReturnMultipleValues( "Hello, World!", @outStr, @outCount )
print outStr; ". The combined strings have ";
HandleEvents
</syntaxhighlight>
Line 1,255:
Another way to pass multiple values from a function is with records (AKA structures):
<syntaxhighlight lang="text">
// Elements in global array
_maxDim = 3
begin record Addresses
end record
begin globals
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) )
for i = 0 to 2
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">
// Elements in global array
_maxDim = 3
begin globals
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 ) )
for i = 0 to 2
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">
begin globals
// An FB container can hold up to 2GB of data, contingent on system memory
end globals
Line 1,396 ⟶ 1,393:
// Check the new results
print gC1 : print gC2
HandleEvents
</syntaxhighlight>
|