Introduction to Methods
Introduction
Basics Of Java
Introduction

 

Introduction

footer line

A function is a collection of statements or a block of code that is used to perform some specific task. It is used to reuse the code without retyping the code. We can write the function once and use it many times. We don’t need to write the code again and again. The function is executed only when we call it. In Java, a function is also called a method. 

 

Method Declaration in Java: The method declaration in java provides information about the method attributes such as method name, method return type, method visibility, method parameters, and the method body. Let’s look at the example of the method declaration.

 

public int max(int x, int y) {
    // Method body
}

 

Here,

  • public: the public is a modifier.
  • int: int is a return type of method that means it returns an int value.
  • max: max is the name of the method.
  • int a, int b: int a, and int b is the list of parameters

 

Example of Method Declaration: In this example, we declare a method that accepts two parameters and returns the maximum of two numbers. Let’s look at the example:

 

public int max(int x, int y) {

    if(x > y)
       return x;
    else
        return y;
}

 

Method Calling in Java: Once we declare a method, we need to call this method to perform some specific task. When we call a method,  the program controls transfer to the called method. Let’s look at the example.

 

public class Example {

    // Function to return a maximum of two numbers
    public static int max(int x, int y) {
    
    	if(x > y)
    		return x;
    	else
    		return y;
    }

    // Driver Method
    public static void main(String arg[]) {
       int a = 10;
       int b = 20;
  
       // Method Calling
       int maximum = max(a, b);
       System.out.println(maximum);
    }
}

 

Output:

 

20

 

How does function calling work?

 

Java Methods (With Examples)

Consider the following code where there is a function called findSum which calculates and returns the sum of two numbers.

 

public class Solution {
	
	public static int findSum(int a, int b) {
		int sum = a + b;				// Line number A
		return sum;
	}
	
	public static void main(String args[]) {
		
		int a = 10;
		int b = 20;
		int c = findSum(a, b);		// Line number B
		System.out.println(c);
	}
}

 

The function being called is called callee(here it is findSum function) and the function which calls the callee is called the caller (here the main function is the caller). When a function is called, program control goes to the entry point of the function. The entry point is where the function is defined. So focus now shifts to the callee and the caller function goes in paused state. 


For Example: In the above code entry point of the function findSum() is at line number A. So when at line number B the function call occurs the control goes to line number A, then after the statements in the function findSum() are executed the program control comes back to line number B.

 

Why do we need functions? 

  • Reusability:  A function can be reused once it has been defined. The function can be called as many times as necessary, which saves time. Consider the situation where you must calculate the circle's area. Now you can either use the formula to find the circle area any time or create a function to find the circle area and call it whenever you need it.
     
  • Neat code: A code created with a function is easy to read and dry run. You don’t need to repeatedly type the same statements; instead, you can invoke the function whenever needed.

 

  • Modularisation: Functions help in modularizing code. Modularisation means dividing the code into small modules, each performing a specific task. Functions allow in doing so as they are the program’s tiny fragments designed to perform the specified task.
     
  • Easy Debugging: It is easy to find and correct the error in functions compared to raw code without functions where you must correct the error everywhere the specific task of the function is performed.