Why are Functions Used?
When we break up a huge problem into smaller subproblems, and we want to increase the readability of the code and to increase usability we divide our solution into classes containing methods.
When a set of statements or functionality is needed, again and again, we put it into functions/methods.
Today we will learn how to declare, define and call the functions/methods in Java and different type of functions.
There are two types of functions in Java as we already explained for C#:
- Built-in functions
- User Define Functions
Built-in Functions:
They are pre-defined functions in the Java libraries. Like String functions, math functions, JFrame functions, etc.
User Defined Functions:
When a user writes a set of statements to reuse again and again or to increase the readability of the code.
Click here to download Simple Calculator Example in Java
Today our focus is on user-defined functions, so first see the basic syntax of user defined functions in Java.
Syntax:
public static void function_name ( ) {
}
- Public static: It is known as a modifier and defines the access class and scope of the function. Static is a keyword used to make the classes accessible through its class name.
- void: return type and in that case it is void, means this function returns nothing.
- Function_Name: Any name of the function.

A function has three parts:
- Declaring a function also called function signature
- Defining a function
- Calling a Function
Signature of function:

It is not necessary, but it is when we have a function definition at some other place or we want to put all of our code info in the start of the program, like in the case of an abstract class or interface.
Syntax:
public static void laptop(int, String);
The above example shows that there is a function in the code with name laptop and it contains two arguments first has type integer and the second one is a String.
Defining Function:

The function definition describes what this function does. The main part of the function and without this the function has no meaning.
Example:
1 2 3 4 5 |
public static void laptop(){ System.out.println("This Function is named as Laptop"); } |
Calling a Function:

To call the function, there is no big logic. Just write the function name with brackets and a semicolon like
1 |
laptop(); |
If the function contains some parameters, then pass the arguments of the same data type in the same sequence as the function is following:
public static void laptop(int id, String name);
This function call must have two arguments in the same style like:
1 |
laptop (1 , ” DELL ”) ; |
There is another type of function that is called the constructor, which we discussed with the concept of classes.
Some Examples:
Function returns the sum of two numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.*; public class MyFirstProject{ public static int sum(int a,int b){ return (a+b); } public static void main(String args[]){ int result = sum(5,7); System.out.println("The result of Sum is = "+result); } } |
Function Overloading and overriding:
Function overloading means the function has the same name with different return type and its also a different number of parameters and also the same number of parameters with different data type. Function overriding means functions with the same names and same return types, but in inherited classes.
Example:
public static int myfunction (int a);
public static void myfucntion (int a);
public static void myfunction (String name);
public static void myfunction (String name, int Id);
the above all example have same function name, but these all have something different to each other and due to this they are overloaded on each other.
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class MyFirstProject{ public static int sum(int a,int b){ return (a+b); } public static int sum(int a,int b,int c){ return (a+b+c); } public static void main(String args[]){ int result = sum(5,7); System.out.println("The result of Sum of 2 numbers = "+result); result = sum(5,7,3); System.out.println("The result of Sum of 3 number = "+result); } } |
Note: Like C++ there is no inline function in Java.
Function Returning Nothing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class MyFirstProject{ public static void myfun(){ System.out.println("This function return nothing"); } public static void main(String args[]){ myfun(); } } |
Pass function as a Parameter or an Argument:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.*; public class MyFirstProject{ public static int sum(int a,int b){ return (a+b); } public static int mul(int a,int b){ return (a*b); } public static void main(String args[]){ int result = sum(5, mul(2,4)); //function passed as an argument or parameter System.out.println("The result of (2*4)+5= "+result); } } |
Click here to download Simple Calculator Example in Java