Lecture 22 (Sherriff) - Methods

Lecture Date: Wednesday, March 4

Let’s first review the method header (also called the signature):

public static int addNumbers(int a, int b)

public: This defines whether the method is visible or not in other parts of the program. For this class, all of your methods will be public.
static: This indicates whether the method is owned by the class itself or by an instance of the class. So, Math.pow(int x, int y) is static because it is called on the class name itself, while myTurtle.forward(50) is not static because it is called on a particular instance of that class.
int: In this example, int is our return type. It is what is “given back” after the method has completed. If a method does not return anything, the type here is void.
addNumbers: This is the method’s name. Two methods can have the same name, if and only if they have different parameters.
(int a, int b): These are the parameters (or arguments) that are given to the method when it is called. They are typically used as input to whatever the method is executing.

Let’s consider some different types of methods:

Methods might or might not return a value
As mentioned above, all methods have a return type - that’s the thing right before the name of the method. However, we could see a data type there that we know (like int or String) or we could just see void, like we do with main. All methods return - that’s what happens when they reach the end or finish by using the keyword return. The difference is some bring back to the place where they were called some value of something. The return type listed in the method header provides.

Library methods are static
The point of a library, like Math is to provide a set of functionality with methods that you can use in your program without creating a particular object to do it. This is what it means for a method to be owned by the class, not by an instance of the object. A non-static method is one that has to operate on a particular thing, like a specific Turtle or Scanner you created.

Some methods have effects, even if they are void
Consider println(String txt). This method is technically void - it does not return anything. However, it does print stuff to the screen. A method can have an effect on the system even if it does not return a value. We will see this more when we consider how parameters are passed by value or passed by reference.

When you invoke a method, the following happens in order:

  1. A new piece of memory (called a “stack frame” or “activation record”) is created.
  2. The parameters of the method are created in that new memory.
  3. The arguments you pass in to the method invocation get copied into the new memory’s parameters in order.
  4. We note where we left off running the old method
  5. We stack the new memory on top of the old memory, covering up the old memory completely
  6. We start running the code in the new method’s body

When you return from a method, either with the return keyword or by reaching the end of the body, the new memory is removed (“popped”) from the stack, leaving us with the calling method’s memory.

We’ll do some examples of how methods get put on the stack.

Slides

(There may not be audio for this lecture.)