Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind C++ Pass Structures to a Function?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ Car {3Order
Put the learning moves in the order that makes the concept easiest to apply.
This is useful when you want to work with grouped data inside a function:
You can also pass a structure to a function.
Pass Structure 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.