Functions in Java

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.
Signature of functions
Signature of functions

A function has three parts:

  • Declaring a function also called function signature
  • Defining a function
  • Calling a Function

Signature of function:

Signature of 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:

Definition and body of function
Definition and body of function

The function definition describes what this function does. The main part of the function and without this the function has no meaning.

Example:

Calling a Function:

Calling a function with parameters
Calling a function with parameters

To call the function, there is no big logic. Just write the function name with brackets and a semicolon like

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:

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:

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:

Note: Like C++ there is no inline function in Java.

Function Returning Nothing:

Pass function as a Parameter or an Argument:

Click here to download Simple Calculator Example in Java