Passing structs to Functions

struct variables can be used like any other variable type. If p1 and p2 are both of type Person, then p1 = p2 will copy all the members of p2 to p1. struct variables can be passed as arguments to functions; by default they are passed by value. The function int getx(Point p) { return p.x; } returns the x coordinate of a Point p. The function call getx(p1) will copy the actual argument (p1) into the formal argument (p). It is also possible to pass a struct by reference. Such arguments can be very convenient ways to pass a lot of information to a function. Functions may also return struct values.

Using Structures Instead of Many Parameters

Functions that have too many arguments are not only difficult to type but easy ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.