JAVA-Functions - Notes By ShariqSP

Methods (Functions) in Java

In Java, a method, also known as a function, is a block of code that performs a specific task. Methods help in organizing code, improving readability, and promoting reusability.

Why Do We Need Methods?

Methods provide a way to encapsulate code for reuse, which helps in reducing redundancy and improving maintainability. They also enable modular programming, allowing complex tasks to be broken down into smaller, more manageable parts.

Syntax of a Method:

access_modifier return_type method_name(parameter_list) {
                  // Method body
                  // Statements
                  return return_value; // optional
              }
              

Keywords Used in Method Declaration:

  • access_modifier: Specifies the visibility or accessibility of the method (e.g., public, private, protected).
  • return_type: Specifies the data type of the value returned by the method. If the method does not return any value, use void.
  • method_name: Name of the method, which is used to call it later.
  • parameter_list: Comma-separated list of parameters (input) that the method accepts. Each parameter consists of a data type and a parameter name.
  • return_value: The value returned by the method, if any. This is optional and depends on the return type specified.

Example of a Method:

public class Example {
                // Method definition
                public static int add(int num1, int num2) {
                  return num1 + num2;
                }
              
                // Main method (entry point of the program)
                public static void main(String[] args) {
                  int result = add(5, 3); // Calling the add method
                  System.out.println("Result: " + result);
                }
              }
              

Function Execution Process:

When a Java program is executed, it starts execution from the main() method. If a method is called within main() or any other method, the control jumps to the called method, executes its statements, and then returns to the calling method.

  • Calling Function: The function from which another function is invoked is called the calling function.
  • Calling Statement: The statement that invokes another function is called the calling statement.
  • Called Function: The function that is invoked by another function is called the called function.

Example of Function Call:

public class FunctionExample {
                // Method definition
                public static void display() {
                  System.out.println("Inside display() method");
                }
              
                // Main method (entry point of the program)
                public static void main(String[] args) {
                  System.out.println("Calling display() method");
                  display(); // Calling the display method
                }
              }
              

In this example, main() is the calling function, the statement display(); is the calling statement, and display() is the called function.

quiz Long answers