Ad space (banner)
๐Ÿ”งC Lessons
Lesson 47 / 68

Functions That Return Multiple Values (Structs)

This lesson covers returning multiple values from a single function in C using a struct, so you understand a function isn't limited to a single return value. It's for anyone searching "C struct multiple return values."

A C function can only directly return one value, but returning a struct that bundles several values effectively lets you return multiple. Use it when you want to return several related results together, like a minimum and maximum.

The example defines a type, typedef struct { int min; int max; } MinMax;, and the minMax() function returns this struct, returning the minimum and maximum together. The caller can then access result.min and result.max.

A common early mistake is not knowing when to use this versus a pointer-based approach. There are two ways โ€” returning a struct wholesale, or writing multiple values through pointer arguments โ€” and you'd choose based on your purpose.

In real projects, this is genuinely useful for things other languages would handle with a tuple or pair โ€” the standard C convention is to express it with a struct instead.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)