codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

29 / 87 topics
28Structures (struct)29Structure and Function30Pointers to Structure31Enumerations (enum)32Unions
Tutorials/C++ Programming/Structure and Function
⚡C++ Programming

Structure and Function

Updated 2026-05-12
30 min read

Structure and Function

In the previous section, we explored the basics of structures in C++. Structures are user-defined data types that allow us to combine different types of variables into a single unit. Now, let's delve deeper into how we can use structures within functions by passing them as arguments and returning them.

Introduction

Passing and returning structures in functions is a powerful feature in C++ that enhances code modularity and reusability. By doing so, you can encapsulate complex data handling within functions, making your programs more organized and easier to manage. Understanding how to pass structures by value and by reference is crucial for efficient memory usage and performance.

Passing Structures to Functions

Passing Structures by Value

When a structure is passed by value to a function, a copy of the entire structure is created and passed to the function. This means that any modifications made to the structure inside the function do not affect the original structure outside the function.

Example: Passing Structure by Value

C++
1#include <iostream>
2using namespace std;
3
4struct Rectangle {
5 int length;
6 int width;
7};
8
9void printArea(Rectangle r) {
10 cout << "Area of rectangle: " << r.length * r.width << endl;
11}
12
13int main() {
14 Rectangle rect = {5, 3};
15 printArea(rect);
16 return 0;
17}
Output

Here, the scaleRectangle function takes a reference to a Rectangle. The original rect in main is modified directly, as shown by the output.

Returning Structures from Functions

Returning structures from functions allows you to create and return complex data types directly. This can be particularly useful when you need to perform calculations or transformations on a structure and want to return the result.

Example: Returning Structure from Function

C++
1#include <iostream>
2using namespace std;
3
4struct Rectangle {
5 int length;
6 int width;
7};
8
9Rectangle createRectangle(int l, int w) {
10 Rectangle r = {l, w};
11 return r;
12}
13
14int main() {
15 Rectangle rect = createRectangle(4, 6);
16 cout << "Length: " << rect.length << ", Width: " << rect.width << endl;
17 return 0;
18}
Output

This program demonstrates creating a rectangle, printing its area, scaling it, and printing the new area. It showcases how structures can be effectively managed using functions.

Summary

  • Passing by Value: Creates a copy of the structure, modifications do not affect the original.
  • Passing by Reference: Directly modifies the original structure, more memory efficient.
  • Returning Structures: Allows functions to return complex data types directly.
ConceptDescription
Passing by ValueCopies the structure, changes do not affect the original.
Passing by ReferenceModifies the original structure directly, more efficient for large structures.
Returning StructuresFunctions can return complex data types like structures.

What's Next?

Now that you understand how to pass and return structures in functions, the next step is to learn about pointers to structures. Pointers provide a way to access and manipulate structures more flexibly, which will be covered in the next topic.

Stay tuned for more advanced C++ concepts!


PreviousStructures (struct)Next Pointers to Structure

Recommended Gear

Structures (struct)Pointers to Structure