Table of contents
The function is a block of code
that is to be executed when it is called.
You can pass the data
which is nothing but passing the parameters
inside the method.
Methods can also perform certain actions and we call them as functions.
Methods can be useful to reuse the code
many times.
In Functions, we need to define
the method code once
and use it many times
.
A function has the following syntax:
[modifiers] [return type] functionName(parameter list) {
// function body
}
Create a Method:
A method
that should be declared within a class
.
It is defined with the name of the method and followed by parentheses ()
.
//create a method inside the main
public class Main {
static void myMethod() {
// code to be executed
}
}
Call a Method:
To call a method in java, we need to provide the methods name followed by two paratheses( )
and a semicolon ;
public class Main{
static void mymethod(){
System.out.println("Success");
}
public static void main(String[] args){
mymethod();
}
}
ย