Return multiple values: Difference between revisions

Line 9:
}</lang>
 
=={{header|C|C}}==
C has structures and unions which can hold multiple data element of varying types. Unions also use the same memory space for storing different types of data. That's why for the union, the num comes out as 67 instead of 99. 67 is the value of 'C' in ASCII.
<lang c>
#include<stdio.h>
 
typedef struct{
int integer;
float decimal;
char letter;
char string[100];
double bigDecimal;
}Composite;
typedef union{
int num;
char letter;
}Zip;
 
Composite example()
{
Composite C = {1,2.3,'a',"Hello World",45.678};
return C;
}
 
Zip example2()
{
Zip r;
r.num = 99;
r.letter = 'C';
return r;
}
 
 
int main()
{
Composite C = example();
Zip rar = example2();
printf("Values from a function returning a structure : { %d, %f, %c, %s, %f}",C.integer, C.decimal,C.letter,C.string,C.bigDecimal);
printf("\n\n\nValues from a function returning a union : { %d, %c}",rar.num,rar.letter);
return 0;
}
</lang>
Output:
<lang>
Values from a function returning a structure : { 1, 2.300000, a, Hello World, 45.678000}
 
 
Values from a function returning a union : { 67, C}
</lang>
=={{header|C++}}==
The new 2011 C++ standard includes tuples, which allow a number of different values of even different types to be passed around.
503

edits