bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

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