Function in Java
Definition of Function/ Method / Sub-routine / Procedure
Function is reusable block of code to perform some specific task.
Java is object oriented language so function is defined in a class and it executes when a
particular function is called or invoked.
Advantages of function:-
● Code Reusability: Write once, use multiple times without repeating code so that code reusability increase.
● Modularity: Dividing a program into separate methods allows each method to handle a specific task,
making the code more organized and easier to manage.
● Readability: Smaller, named methods make the code easier to read and understand.
● Maintainability: It’s easier to fix bugs or update code when it's organized into methods.
Types of Function:-
Predefined Method User Defined Method
The function which is created by user to
Predefined methods are the method that is
already defined in the Java class libraries. It is carry out some specific task is known as
also known as the standard library method or user defined function.
built-in method.
To write program using method the following
For example, random() method which is present
steps are needed:-
in the Math class and we can call it using the
[Link]().
1) Function Prototype / Function
[Link]() // returns random value declaration
[Link] //returns the value of π 2) Function Definition
as a constant. it is a Constant not a 3) Function Calling / Invoking / Executing
Method
Function Prototype / Function Declaration
A function prototype is a declaration of a function that informs the compiler about its name, return type, and
parameters before the function is actually used or defined.
It is very first line of Function Definition.
The prototype describes the status of the function that includes
1) Access Specifier
2) Modifier
3) Function return data type
4) Function Name
5) List of parameters
Syntax:-
[<Access specifer>] [<modifier>] return_data_type Function_Name(list of parameters)
Note:- The square brackets ‘[ ]’ indicates these are optional
Access Specifier
Access specifier in Java are used to control the
visibility and accessibility of classes, methods, and
variables. They help enforce encapsulation by
restricting access to different parts of a program.
Java provides four types of access modifiers to
define scope and protection levels.
● Public modifier: It is accessible from
anywhere in the program
● Protected modifier: It is accessible within
the same package and by subclasses
● Private modifier: It is accessible only
within the same class
● Default modifier: It is accessible only
within the same package
Modifiers in Java are keywords that change Return_data_type
the meaning or behavior of classes,
● If void is used as return data type,it means
methods, variables, etc.
function is not returning any value
Modifier may be static or final or native or ● If int or float or long or double or char or
volatile etc. boolean is used as return data type , it
means the function returns a value of the
The static keyword makes a method as a
corresponding data type.
class function, this means the function can
● A function can return only one value.
be called or invoked or executed through the
class name and not through the object of
the class.
The final keyword changes the function into
final or constant which means statment of
function cannot be modified once it is
declared final.
Function_Name:- It is defined by user following rules of identifier.
List_of_parameters:- it includes name of variables along with the data type
separated by commas and are used within function to perform [Link]
are also known as method signature/ function signature.
Note:-The method signature is the unique identifier of a method. It consists of:
● Method Name
● Number, Type, and Order of Parameters
● Method Overloading is based on method signature.
Important: Return type and access specifier is NOT part of the method signature.
Example:-public int add(int a, int b) { ... } // Signature: add(int, int)
public double add(double a, double b) { ... } // Signature: add(double,
double)
public int add(int a, int b, int c) { ... } // Signature: add(int, int, int)
public void add(int a, String b) { ... } // Signature: add(int, String)
Difference Between Static and Non-Static Methods in Java
Function Definition
It is the extension of function prototype that contains set of statements within
the curly braces to perform some task.
syntax:-
[access_specifier] [modifiers] return_data_type method_Name(parameterList)
{
// method body
// statements
return value; // if return type is not void
}
The return statement:-
The return keyword is used to return a value from the function definition to function
control statement.
Syntax:-
return (value);
Note:- a function can return only one value at a time.