Section 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
drawRectangleso that the function could control and use whatever turtle instance we passed to it.
Here is a simple function called
distanceinvolving our new
Pointobjects. The job of this function is to figure out the distance between two points.
distancetakes two points and returns the distance between them. Note that
distanceis 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
distanceis not a method of Point is that
selfis not included as a formal parameter. In addition, we do not invoke
distanceusing the dot notation.
You have attempted of activities on this page.