bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/C++/C++ Functions
C++•C++ Functions

C++ Pass Structures to a Function

Pass Structure to a Function

You can also pass a structure to a function.

This is useful when you want to work with grouped data inside a function:

Example

struct Car {
  string brand;
  int year;
};
void myFunction(Car
c) {
  cout << "Brand: " << c.brand << ", Year: " << c.year << "\n";
}
int main() {
  Car myCar = {"Toyota", 2020};
  myFunction(myCar);
  return 0;
}

Note

Since the structure is passed by value, the function gets a copy of the structure.

This means that the original data is not changed.

Previous

C++ Pass Array to a Function

Next

C++ Function Overloading

This chapter

Overview
11

Lessons

26m

Read time

1. C++ Functions2. C++ Function Parameters3. C++ Default Parameters4. C++ Multiple Parameters5. C++ The Return Keyword6. C++ Pass Array to a Function7. C++ Pass Structures to a Function8. C++ Function Overloading9. C++ Variable Scope10. C++ Recursion11. C++ Lambda Functions