Class-First Coding
© 22 Oct 2013 Luther Tychonievich
Licensed under Creative Commons: CC BY-NC-ND 3.0
other posts

Learning to Program

Lesson 2: Methods

 

Conceptual Overview

In the last lesson we introduced the idea of a method as a description of an action associated with an object, but did not go into any detail. That detail is the subject of this lesson.

Defining a method is like writing a how-to or a recipe. And like most how-tos or recipes, there is some context we need before we get into the step-by-step directions. In particular, every method needs to define what parts and pieces you have to bring to the table and what kind of thing you’ll be creating.

Following the steps of the how-to or recipe is called invoking the method. When I define a recipe I might say “‍you need 2 eggs‍” and “‍creates two dozen cookies‍”; when I invoke the recipe I need to actually have two physical eggs with me and I’ll end up with actual cookies. Harking back to the vocabulary of last lesson, I define a method in terms of classes; I invoke it using instances of those classes.

In programming vocabulary, the thing a method creates is called a return value. Not all methods return something; the accelerate method of a car, for example, would change the car’s speed but wouldn’t return anything to the person who decided to invoke the method. Invoking the isZero method of a fraction wouldn’t change the fraction, but would return a true or false value (which we learned last time is called a boolean or bool). If a method returns nothing, we say “‍its return type is void‍” or “‍it is a void method‍”.

In programming vocabulary, the things a method needs the invoker to supply are called the parameters of the method definition the things the invoker actually supplies are called the arguments of the method invocation. Parameters describe the type of each thing we need; arguments are instances of those types.

Most programming languages allow many parameters but only one return per method.

Coding Method Definitions

In most C-derived object-oriented languages, including C++, Java, C#, and D, a class is specified by writing its return type, then its name, then it’s parameters inside parentheses, and finally a description of what it does inside braces. The parameters are specified like fields: type followed by name (but no semi-colons). For example,

class Car {
Point location;
double speed;
double heading;
void changeSpeed(double newSpeed) {
set this Car’s speed to be newSpeed
}
double speedInMPS() {
convert this Car’s speed to meters per second
and return the result

}
boolean isBetween(Point p1, Point p2) {
consider the triangle connecting the points
this Car’s location, p1, and p2.
Return true if the p1-to-p2 edge is longest.

}
}

There are several observations to make here.

The portion of the method definition that precedes the { is called the method header; the part inside the braces is the method body. There are rules about how to write method bodies; we’ll get into them in a later lesson.

Coding Method Invocations

Method invocations are written as

instance . method ( argument1, argument2, … ) This notation varies a little; you might be able to leave off the “‍instance .‍” in some languages and might have to use -> instead of . in others.

The order of the arguments is important: the first argument will always be used as the value of the first parameter.

The invocation can be used as if it was the value it returns. So if x was an instance of the Fraction class representing the fraction
13
3
, and if the Fraction class had a method named nearestInteger, then we could write, e.g., 2 + x.nearestInteger() - 3 which (assuming that the method nearestInteger does what its name suggests) would mean the same thing as 2 + 4 - 3.




Looking for comments…



Loading user comment form…