Functions
Functions
Functions
Methods
A program that provides some functionality can be long and contains many statements
// Method body;
}
Method Declaration
Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.
protected: accessible within the class in which it is defined and in its subclass(es)
default (declared/defined without using any modifier) : accessible within same class and package within which its class is defined.
The return type : The data type of the value returned by the the method or void if does not return a value.
Method Name : the rules for field names apply to method names as well, but the convention is a little different.
Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there
are no parameters, you must use empty parentheses ().
Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).
Method body : it is enclosed between braces. The code you need to be executed to perform your intended operations.
Basic Syntax
Method signature
: A method name is typically a single word that should be a verbin lowercase or multi-word, that
begins with a verb in lowercase followed by adjective, noun….. After the first word, first letter
of each word should be capitalized. For example, findSum,
computeMax, setX and getX
Generally, A method has a unique name within the class in which it is defined but sometime a
method might have same name as other method name within the same class as method
overloading is allowed in Java.
Method Declaration: Header
class MyClass
{ …
static int min ( int num1, int num2 )
1. Return a value
next = keyboard.nextInt();
It just depends on what you want to accomplish! You create a method when you want to group a block of
code together that usually does a certain task and can be used multiple times which prevents code
repetition. You would only need to 'return' a value when you need to get something from a method as
values that are created in methods are stored there locally.
For example, if I wanted to create a method which would print a String that I pass in as a parameter, I would write
System.out.println(str);
In this case, there is no value that I would need to use somewhere else in the program.
Anyone explain to me in which instance a void method is used vs. a value
returning method (or vice versa)
Now, say I wanted to create a method that would take the sum of two numbers that I pass in as parameters and then be able to use that result, I would write
return x + y;
In this case, I could assign the method findSum(3, 5) to a variable or use it in any other way I would need and it would be equal to whatever it is returning.
}
Difference between loops and functions
functions: a function is a piece of code you can run over and over again simply by calling
it. All methods are functions, but not all functions are methods. You can have a function
that isn't tied to a class and is sort of free-floating in your code and will execute every
time you call it. But all functions defined within a class are called methods.
Loops: A loop is a piece of code that runs over and over again until the condition is false.
Or in the case of a for in loop until the number of items to iterate over has reached an
end. A loop can be just about anywhere in your code. It can even be inside a function or
method.
public void bar(int num1,int num2) {
System.out.print(num1+num2);
int a = 3; int b = 5;
bar(a, b);
You can picture in your head that when bar(a, b) is run, it's like in the beginning of bar the following two lines are written: