Multiple Return Values in C
Our program demonstrates the concept of multiple return values. While C doesn’t have built-in support for multiple return values like some modern languages, we can achieve similar functionality using pointers or structures. Here’s an example implementation:
In this C implementation:
We define a
struct Values
to hold multiple return values.The
vals()
function returns an instance ofstruct Values
containing two integers.In the
main()
function, we callvals()
and access the returned values through the struct members.To demonstrate ignoring one of the values (similar to the blank identifier
_
in some languages), we simply don’t useresult2.a
.
To compile and run this program:
While C doesn’t have native support for multiple return values, using structures or pointers allows us to return multiple values from a function in a clean and organized manner.