Return multiple values: Difference between revisions

add BLC solution
m (→‎{{header|FutureBasic}}: Remove the unsupported 'ConsoleWindow')
imported>Tromp
(add BLC solution)
 
(12 intermediate revisions by 9 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" paremetersparameters on the method. This can be in addition to the value returned by the method.
<syntaxhighlight lang="c sharp">using System;
using System.Collections.Generic;
Line 900 ⟶ 974:
diff = a - b
.
call addSubtract 7 5 sum diff
print "Sum: " & sum
print "Difference: " & diff
Line 955 ⟶ 1,029:
 
=={{header|Elena}}==
ELENA 56.0x :
<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'''
<syntaxhighlight lang="euler">
'''new''' mv; '''new''' getMV;
begin
new mv; new getMV;
getMV &lt;- ` '''formal''' v; ( v, v * v, v * v * v ) &apos;;
 
getMV <- ` formal v; ( v, v * v, v * v * v ) ';
mv &lt;- getMV( 3 );
 
mv <- getMV( 3 );
'''out''' mv[ 1 ];
 
'''out''' mv[ 12 ];
'''out''' mv[ 23 ];
'''end''' $
out mv[ 3 ]
end $
</syntaxhighlight>
 
=={{header|Euphoria}}==
Line 1,421 ⟶ 1,515:
 
{{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,645 ⟶ 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.
Returning multiple values is natural. There are no limits to which parameters may be used for inputs and which parameters may be used for outputs. It's all the same to Plain English. You choose which slot(s) to put your return value(s) into.
 
It is possible, however, to call an auxiliary routine to change fields in a record and:
Notice that the outputs actually come before the inputs in this case. (You could even have the outputs overwrite the inputs if you want.) In the calling scope, <code>a product</code> and <code>a quotient</code> introduce new local variables containing the values placed into them by the routine.
<syntaxhighlight lang="plainenglish">To run:
Start up.
Compute a product and a quotient given 15 and 3.
Write "The product is " then the product on the console.
Write "The quotient is " then the quotient on the console.
Wait for the escape key.
Shut down.
 
# Concatenate these values, assigning the concatenated string to a new string (see code below);
A product is a number.
# 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
To compute a product and a quotient given a number and another number:
Put the number times the otherA number intocalled thefirst product.field,
A string called second field,
Put the number divided by the other number into the quotient.</syntaxhighlight>
A flag called third field.
{{out}}
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
The product is 45
The quotient is 5
</pre>
 
Line 3,302 ⟶ 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="ecmascriptwren">var splitName = Fn.new { |fullName| fullName.split(" ") }
 
var names = splitName.call("George Bernard Shaw")
Anonymous user