The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Structures as Parameters

You can pass structures as parameters in the usual way. For example,

void printPoint (Point p) {
  cout << "(" << p.x << ", " << p.y << ")" << endl;

printPoint takes a point as an argument and outputs it in the standard format. If you call printPoint (blank), it will output (3, 4).

As a second example, we can rewrite the distance function from Section 5.2 so that it takes two Points as parameters instead of four doubles.

double distance (Point p1, Point p2) {
  double dx = p2.x - p1.x;
  double dy = p2.y - p1.y;
  return sqrt (dx*dx + dy*dy);

Last Update: 2005-12-05