17.7. Objects as Arguments and Parameters

You can pass an object as an argument in the usual way. We’ve already seen this in some of the turtle examples where we passed the turtle to some function like drawRectangle so that the function could control and use whatever turtle instance we passed to it.

Here is a simple function called distance involving our new Point objects. The job of this function is to figure out the distance between two points.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

distance takes two points and returns the distance between them. Note that distance is not a method of the Point class. You can see this by looking at the indentation pattern. It is not inside the class definition. The other way we can know that distance is not a method of Point is that self is not included as a formal parameter. In addition, we do not invoke distance using the dot notation.

You have attempted 1 of 2 activities on this page