Return multiple values: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 52:
(+ x y))</lang>
<br><br>
 
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<lang Action!>CARD EndProg ;required for ALLOCATE.ACT
 
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
 
DEFINE PTR="CARD"
DEFINE RECORD_SIZE="6"
TYPE Record=[CARD min,max,sum]
 
PROC ArgumentsAsPointers(CARD ARRAY a BYTE n CARD POINTER min,max,sum)
BYTE i
 
min^=65535 max^=0 sum^=0
FOR i=0 TO n-1
DO
IF a(i)>max^ THEN
max^=a(i)
FI
IF a(i)<min^ THEN
min^=a(i)
FI
sum^==+a(i)
OD
RETURN
 
PROC ArgumentAsRecord(CARD ARRAY a BYTE n Record POINTER res)
BYTE i
 
res.min=65535 res.max=0 res.sum=0
FOR i=0 TO n-1
DO
IF a(i)>res.max THEN
res.max=a(i)
FI
IF a(i)<res.min THEN
res.min=a(i)
FI
res.sum==+a(i)
OD
RETURN
 
PTR FUNC ResultAsRecord(CARD ARRAY a BYTE n)
Record POINTER res
BYTE i
 
res=Alloc(RECORD_SIZE)
res.min=65535 res.max=0 res.sum=0
FOR i=0 TO n-1
DO
IF a(i)>res.max THEN
res.max=a(i)
FI
IF a(i)<res.min THEN
res.min=a(i)
FI
res.sum==+a(i)
OD
RETURN (res)
 
PROC Main()
CARD ARRAY a=[123 5267 42 654 234 6531 4432]
CARD minV,maxV,sumV
Record rec
Record POINTER p
 
Put(125) PutE() ;clear screen
AllocInit(0)
 
ArgumentsAsPointers(a,7,@minV,@maxV,@sumV)
PrintE("Return multiple values by passing arguments as pointers:")
PrintF("min=%U max=%U sum=%U%E%E",minV,maxV,sumV)
 
ArgumentAsRecord(a,7,rec)
PrintE("Return multiple values by passing argument as pointer to a record:")
PrintF("min=%U max=%U sum=%U%E%E",rec.min,rec.max,rec.sum)
 
p=ResultAsRecord(a,7)
PrintE("Return multiple values by returning a pointer to a record:")
PrintF("min=%U max=%U sum=%U%E",p.min,p.max,p.sum)
 
Free(p,RECORD_SIZE)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Return_multiple_values.png Screenshot from Atari 8-bit computer]
<pre>
Return multiple values by passing arguments as pointers:
min=42 max=6531 sum=17283
 
Return multiple values by passing argument as pointer to a record:
min=42 max=6531 sum=17283
 
Return multiple values by returning a pointer to a record:
min=42 max=6531 sum=17283
</pre>
 
=={{header|Ada}}==
Anonymous user