Anonymous user
Return multiple values: Difference between revisions
add BLC solution
imported>Tromp (add BLC solution) |
|||
(14 intermediate revisions by 10 users not shown) | |||
Line 279:
end.</syntaxhighlight>
=={{header|Amazing Hopper}}==
Hopper posee una pila de trabajo de alcance global; luego, los datos pueden ser puestos ahí y accedidos desde cualquier parte del programa.
<syntaxhighlight lang="c">
#include <basico.h>
#proto foo(_X_,_Y_)
algoritmo
_foo(10,1), decimales '13', imprimir
números(v,w,x,y,z, suma)
_foo(0.25,0) ---retener(5)--- sumar todo,
mover a 'v,w,x,y,z,suma'
imprimir(NL,v,NL, w,NL, x,NL, y,NL, z,NL,NL,suma,NL)
terminar
subrutinas
foo(c,sw)
#(c*10), solo si( sw, NL)
#(c/100), solo si( sw, NL)
#(0.25^c), solo si( sw, convertir a notación; NL)
#(2-(sqrt(c))), solo si( sw, NL)
cuando ' #(!(sw)) '{
#( ((c^c)^c)^c )
}
retornar
</syntaxhighlight>
{{out}}
<pre>
100.0000000000000
0.1000000000000
9.536743e-07
-1.1622776601684
2.5000000000000
0.0025000000000
0.7071067811865
1.5000000000000
0.9785720620877
5.6881788432742
</pre>
=={{header|ARM Assembly}}==
Line 420 ⟶ 467:
150 LET C=A+B:LET D=A-B
160 END DEF</syntaxhighlight>
==={{header|uBasic/4tH}}===
{{Trans|Forth}}
uBasic/4tH shares many features with Forth - like a stack. Parameters of functions and procedures are passed through this stack, so there is no difference between pushing the values on the stack ''or'' passing them as function parameters. Return values are passed by the stack as well, so if we push additional values ''before'' calling '''RETURN''' we can retrieve them using '''POP()'''.
<syntaxhighlight lang="qbasic">a = FUNC (_MulDiv (33, 11))
b = Pop()
Print "a * b = ";a, "a / b = ";b
Push 33, 11 : Proc _MulDiv
a = Pop() : b = Pop()
Print "a * b = ";a, "a / b = ";b
End
_MulDiv
Param (2)
Push a@ / b@
Return (a@ * b@)</syntaxhighlight>
{{Out}}
<pre>a * b = 363 a / b = 3
a * b = 363 a / b = 3
0 OK, 0:226 </pre>
=={{header|Binary Lambda Calculus}}==
In the lambda calculus, one can return a tuple, which when applied to a function f, applies f to all the tuple elements. For example, <A,B,C> is <code>\f.f A B C</code>. Alternatively, one can use continuation-passing-style (cps), in which the function f is not applied the tuple return value, but instead is passed as an extra initial argument, and then the function can return f applied to the multiple values.
=={{header|BQN}}==
Line 503 ⟶ 577:
=={{header|C sharp|C#}}==
The preferred way to return multiple values in C# is to use "out"
<syntaxhighlight lang="c sharp">using System;
using System.Collections.Generic;
Line 900 ⟶ 974:
diff = a - b
.
print "Sum: " & sum
print "Difference: " & diff
Line 955 ⟶ 1,029:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 977 ⟶ 1,051:
console.printLine("Min: ",min," Max: ",max)
}</syntaxhighlight>
=== Using Tuples syntax ===
<syntaxhighlight lang="elena">import system'routines;
import extensions;
extension op
{
::(int, int) MinMax()
{
var ordered := self.ascendant();
^ (ordered.FirstMember, ordered.LastMember);
}
}
public program()
{
var values := new int[]{4, 51, 1, -3, 3, 6, 8, 26, 2, 4};
(int min, int max) := values.MinMax();
console.printLine("Min: ",min," Max: ",max)
}</syntaxhighlight>
{{out}}
Line 1,038 ⟶ 1,134:
<br>
Lists are constructed by placing the values between ( and ). Once assigned to a variable, the list can be subscripted to access the individual elements (which can themselves be lists).
'''begin'''
'''new''' mv; '''new''' getMV;
getMV <- ` '''formal''' v; ( v, v * v, v * v * v ) ';
mv <- getMV( 3 );
'''out''' mv[ 1 ];
'''out''' mv[
'''out''' mv[
'''end''' $
=={{header|Euphoria}}==
Line 1,224 ⟶ 1,318:
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 ⟶ 1,349:
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,379:
fn FillRecord( gAddressData(0) )
for i = 0 to 2
next
HandleEvents
</syntaxhighlight>
Line 1,305 ⟶ 1,398:
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,408:
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,422:
fn FillRecord( gAddressArray( 0, 0 ) )
for i = 0 to 2
next
HandleEvents
</syntaxhighlight>
Line 1,350 ⟶ 1,443:
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,487:
// Check the new results
print gC1 : print gC2
HandleEvents
</syntaxhighlight>
Line 1,421 ⟶ 1,514:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Return_multiple_values}}
'''Solution'''
Every function returns one value. The conventional way to return multiple values is to return a list.
In the following example, the function returns the sum and product of two given numbers.
[[File:Fōrmulæ - Return multiple values 01.png]]
[[File:Fōrmulæ - Return multiple values 02.png]]
[[File:Fōrmulæ - Return multiple values 03.png]]
=={{header|Go}}==
Line 2,650 ⟶ 2,751:
=={{header|Plain English}}==
Since parameters are passed by reference by default in Plain English, returning values ends up being unnecessary in most cases. The only functions that actually return a value are Boolean functions, called deciders.
It is possible, however, to call an auxiliary routine to change fields in a record and:
# Concatenate these values, assigning the concatenated string to a new string (see code below);
# Write the concatenated string on the console; or
# As is done in C style, obtain the address of the record (a pointer) and save the pointer's value in a number for later manipulation.
<syntaxhighlight line="1" start="1">The example record is a record with
A string called second field,
A flag called third field.
To run:
Start up.
Fill the example record.
Write the example record on the console.
Shut down.
To fill the example record:
Put 123 into the example's record's first field.
Put "Hello World!" into the example's record's second field.
Set the example's record's third field.
To Write the example record on the console:
Convert the example's example's record's first field to a string.
Convert the example's example's record's third field to another string.
Put the string then " "
then the example's record's second field
then " " then other string into a return string.
Write the return string on the console.
</syntaxhighlight>When it is necessary to get the return value from a Win 32 API function, the syntax is as follows:
''Call "dllname.dll" "FunctionNameHereIsCaseSensitive" returning a <type>.''
Example:s
''Call "gdi32.dll" "GetCurrentObject" with the printer canvas and 6 [obj_font] returning a handle.''
''Call "kernel32.dll" "SetFilePointer" with the file and 0 and 0 and 2 [file_end] returning a result number.''
''Call "kernel32.dll" "WriteFile" with the file and the buffer's first and the buffer's length and a number's whereabouts and 0 returning the result number.''
''Call "kernel32.dll" "HeapAlloc" with the heap pointer and 8 [heap_zero_memory] and the byte count returning the pointer.''{{out}}
<pre>
123 Hello World! yes
</pre>
Line 3,307 ⟶ 3,434:
=={{header|Wren}}==
In Wren, one would return multiple values from a function or method by using some sort of Sequence object, usually a List though a Map could be used if you needed ''named'' returns.
<syntaxhighlight lang="
var names = splitName.call("George Bernard Shaw")
|