devxlogo

Returning Aggregates from a Function

Returning Aggregates from a Function

Both C and C++ allow you to return by value aggregates, e.g., structs and unions, from a function. However, the runtime overhead of returning large objects by value can be significant. This is why certain compilers issue a warning message when you return an aggregate from a function. For example:

 Date current_date();

In most cases, you can avoid this inefficiency by passing a reference to the aggregate as an argument and write the result into that reference:

 void current_date(Date & d); // improved form

This is more efficient than passing or returning large objects by value.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist