A method
belongs to a particular class and performs a particular task with a grouping of code. It may have arguments
and may return an answer.
A
non-static method is called with dot-notation and acts upon the object
used in the dot-notation. It may access attributes of the object, or it
may change them.
Note: in non-Object-Oriented languages you can also write a function,
which is the a grouping of code like a method, but it does not belong
to a class. Sometimes we use the words interchangeably, but technically
they are different. And sometimes we use the word procedure, although in some languages it is technically different also.
A
block
of code refers to those statements within a set of curly braces ({}).
A method can be re-used with multiple calls.
Modular control
refers to a program that executes primarily as method calls.
A program that consists of mostly method calls is a good example of top-down,
high-level code because the class and method names, if they are named properly,
tell you the major steps being executed.
When a method is called, control is passed to the called method.
The basic syntax for calling a method is dot-notation:
object.methodName(arg1, arg2, ...);
Arguments
allow the calling function to pass values to the called function.
Your arguments must be the same type (or class) that the method expects.
For example, are both of these calls to the Math methods correct?
int base = 2;
int power = 8;
double a;
double b;
a = Math.sqrt(19.5);
b = Math.pow(base, power);
Notice that they are called with Class-dot rather than object-dot.
This
is because they are static methods and do not act upon an object; they
only process the passed-in arguments, so do not need an object.
The
domain
of a function refers to arguments with the proper values, not just the correct datatype/class.
For instance, sqrt() takes a double but -25.0 is an invalid argument because the square-root function is not defined for negative numbers.
A method signature
specifies the method name, the method's return-type, the number of arguments it expects, and the datatypes for those arguments.
The basic syntax for a signaturee is:
returnType methodName(class1 arg1, class2 arg2)
A method can have an many arguments as it needs. The class or data type
listed for each should match the type of the data expected in the function
call.
A method's return type can be a class or primitive data type. It should
match the type of the data that the function returns.
A function that does not return anything has a return type of
void.
Note that function that does not return anything is a void function
and a function that returns a value of zero is an int function. These are NOT the same thing.
The term
argument
refers to the information that the calling procedure passes to the
called method.
The term
parameter
refers to the information that the called method receives from the
calling function.
The two terms refer to the same thing, just from different viewpoints.
The datatype of the argument that you pass must match the
datatype of the parameter that the function expects. (Where is that
specified?)
When a primitive (the datatype starts with a lower-case letter,
like int or double) argument is passed to a function, a copy of its
value is passed in.
This is called
pass-by-value
and because it is a copy, the value of the object used as the
argument cannot be changed.
When an object is passed to the method, the reference is passed. This is called pass-by-reference, and means the object can be changed inside the called method.