Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- A method is a named block of code that runs only when called. A block is any code enclosed in braces.
- Procedural abstraction lets a programmer use a method by knowing what it does without knowing how it was written.
- A parameter is a variable declared in the header of a method. An argument is a value passed into the method when it is called.
- A method signature consists of the method name and the ordered list of parameter types. With no parameters, the list is empty.
- A void method has no return value and is not called as part of an expression. A non-void method returns a value of its return type, which must be stored in a variable or used in an expression to be of any use.
- Arguments must be compatible in number and order with the parameter types, and are passed by call by value: the parameters are initialized with copies of the arguments.
- Overloaded methods have the same name and different signatures.
- A call interrupts sequential execution; when the last statement or a
returnis executed, control goes back to the point immediately after the call.
Key relationships
| Header | Signature | Return type |
|---|---|---|
static int square(int n) |
square(int) |
int |
static double average(int a, int b) |
average(int, int) |
double |
static void greet(String name) |
greet(String) |
void |
static void second() |
second() |
void |
| Call | Meaning |
|---|---|
greet("Ada"); |
void call as a statement |
int s = square(6); |
return value stored |
println(square(2 + 3)); |
return value used in an expression; the argument is an expression too |
int r = greet("x"); |
compile-time error: nothing to store |
square(2, 3) |
compile-time error: wrong number of arguments |
square("3") |
compile-time error: wrong type |
Assumptions and conventions
public staticis treated as part of the pattern in this topic.- Return type and parameter names are not part of the signature, so two methods cannot differ only by return type.
- A primitive parameter that is changed inside a method does not change the argument variable.
- An
intargument can fill adoubleparameter, not the reverse.
Mistakes to avoid
- Putting the return type in the signature.
- Expecting a method to change a primitive argument.
- Using a void method in an expression, or forgetting to store a non-void result you need.
- Mismatching the number, order or type of arguments.
- Defining two methods that differ only in return type.
- Losing track of where control returns: immediately after the call.
- Confusing parameter (in the header) with argument (in the call).
Quick self-check
- What is the signature of
static double avg(int a, int b)? (avg(int, int)) - Can
static int f(int x)andstatic double f(int y)both exist in one class? (No: same signature; only the return type differs.) - After
int n = 5; change(n);wherechangeadds 10 to its parameter, what isn? (Still 5.) - What does
twice(2.5)call if bothtwice(int)andtwice(double)exist? (Thedoubleversion.) - Where does control go when a method finishes? (To the point immediately after the call.)
Next: practice questions.