Methods and Paramenters
Methods and Paramenters
method
We call/invoke the method by using its
identifier
Method to
More than 1 piece of extra information may build a wall
be required
12 7 4
4 9 3 11 -2 -7
}
static void voidMethod2 (int pNum) {
}
MORE EXAMPLE VOID METHODS
static void voidMethod3 (int pNum1, int pNum2) {
……….
}
VALUE METHODS
static int return20 ( ) {
sillyMethod1 (12);
sillyMethod1
sillyMethod1 (48); use 12
sillyMethod1 (20);
sillyMethod1 (0);
sillyMethod1 (-8);
MORE EXAMPLES INVOCATIONS
The following demonstrate different calls to sillyMethod
• Assuming the existence of an int variable called someNumber
• Assuming someNumber has previously been assigned a value 12
sillyMethod1 (someNumber);
sillyMethod1 (someNumber * 4);
sillyMethod1 (someNumber + 8);
sillyMethod1 (someNumber - 12);
sillyMethod1 (someNumber - 20);
MORE ON METHODS
Methods are used to split problems into
smaller tasks
We can identify a method for each of these
simpler, smaller tasks
We can then reuse our method at different
points in the program by ‘calling’ or ‘invoking’
the method
• No limit to the number of times we call/invoke
the method
Methods can have their own variables
• These may only be used/accessed within the
method
• Used to perhaps support the method
SUMMARY
The only role of some methods in life is to ‘return a value’!
• Performs a calculation based on available information and
‘return the result’ of this calculation
• Searches for a value within an array and ‘return the index’ at
which this value was found
• We often refer to such methods as value method
yearBorn = birthYear;
statement;
more statements;
} // name
METHOD – FLOW OF CONTROL
The main method is invoked by the system when you submit
the bytecode to the interpreter
method2();
method1();