What is a method?
A collection of statements grouped together to perform an operation
What are the benefits of using methods?
Define reusable code, organize and simplify coding, make code easy to maintain
What is the syntax for defining a method?
modifier returnValueType methodName(list of parameters) { // Method body; }
What are the two parts of a method?
Method header and method body
What is a method header?
Specifies the modifiers, return value type, method name, and parameters of the method
What is a method body?
Contains a collection of statements that implement the method
What is a formal parameter?
A variable defined in the method header (also called parameter)
What is an actual parameter?
The value passed to a parameter when invoking a method (also called argument)
What is a method signature?
The method name and the parameter list together
What is a value-returning method?
A method that returns a value
What is a void method?
A method that performs operations without returning a value
What keyword is used for void methods?
void
What statement is required in value-returning methods?
A return statement
What is the syntax of a return statement?
return value; or just return; for void methods
Can a void method have a return statement?
Yes, but it’s optional and uses syntax: return;
What is the purpose of return in a void method?
To terminate the method and return to the caller
What modifier is used for all methods in Chapter 6?
static
What are examples of value-returning methods?
max, Math.pow, Math.random
What are examples of void methods?
System.out.println, System.exit, printGrade
What is a caller?
The program that calls/invokes a method
How do you invoke a value-returning method?
As part of an expression: int larger = max(3, 4);
How do you invoke a void method?
As a statement: System.out.println(‘Hello’);
Can a value-returning method be invoked as a statement?
Yes, but the return value is ignored (not common practice)
What is parameter order association?
Arguments must be provided in the same order as parameters in the method signature