Functions in Java

Whether you a professional programmer or just a beginner it is always better to break your programme into blocks, or functions. It will help you later, first of all in debugging, secondly, it is just better to have an elegant, readable and organized code.

A function is a part of a program that has its own name. This name can be used in the program as a command (this command is called a function call). When a function is called, the commands of which it consists are executed. A function call can return a value (similar to an operation) and therefore can be used in an expression along with operations.

Functions are used in programming to reduce its complexity:

  • Instead of writing a continuous sequence of commands in which you soon cease to orient yourself, the program is divided into subroutines, each of which solves a small completed problem, and then a large program is composed of these subroutines (this technique is called decomposition).
  • The total amount of code decreases because, as a rule, one function is used in the program several times.
  • Written once and comprehensively tested function, can be included in the library of functions and used in other programs (it does not need to remember how this function was programmed, it is enough to know what it does).
  • There are many useful libraries of functions that can be used by all programmers, and some libraries come bundled with a programming language (for example, everyone who programmed in Pascal used the library function writeln () to display on the screen, and in Java for these purposes the System.out.println () method is available, which is included in one of the standard libraries).

In object-oriented programing, such as Java a function term is often applied to the method, which is bound to the class and defines the behavior of it. In other words, a function is a piece of code which performs the operation and sometimes returns a value.

A method is a function that is part of a class that can perform operations on data of this class. In the Java language, the entire program consists only of classes and functions can be described only inside them. That is why all functions in the Java language are methods. Int the following paragraphs we shall call functions of Java as methods.

Function/method syntax: 

We need to find max(the biggest) number between two int and return the value of the biggest int. So we can write a function to simplify our work for that:

1.public static int minFunction(int n1, int n2) {
   functionBody
}


2.public static int minFunction(int n1, int n2) {
   int max;
   if (n2 > n1)
      max = n2;
   else
      max = n1;

   return max; 
}

Compartments of functions:

  • public static − modifier,  it defines the access type of the method and it is optional to use.
  • int − return type, function/method may return value as was mentioned before, for example, int
  • minFunction − name of the function, The method/function signature consists of the function/method name and the parameter list
  • n1, n2 − formal parameters, the type, order, and a number of parameters of a method/function. These are optional, method/function may contain zero parameters.
  • int n1, int n2 − list of parameters
  • functionBody-The method/function body defines what the method/function does with the statements.

Method definition consists of a method header and a method body. In Java, all function definitions must be inside classes.

Writing  and Defining function:

Define- write it, including programming instructions and placing the instructions. Argument type and name should be written in a 1st line of the function.

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}

Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:

run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty

Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.

Calling function:

Calling function from another function

  • To call a method/function from another method of the same class, you must specify its name, and then in brackets the list of actual parameters (if the method does not require parameters, brackets are still set). The method call is used as an operation, which can be combined with other operations (in this case, the sum) in the expression. When invoked (called), all the statements that are a part of the method would be executed.
public class Student {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Calling function from another class 

  • To call a method of another class, you must have an object of this class *. The method name is indicated by a period after the object name. For example, the class String (string) has a length () method that returns the length of the string. This method can only be accessed through an object of the String class (which is quite logical, the method is called for the string whose length we want to know).
Student s = new Student(); s.setName("Your_Name"); String name = s.getName();
Don't miss out!
Subscribe To Our Newsletter

Learn new things. Get an article everyday.

Invalid email address
Give it a try. You can unsubscribe at any time.

Comments

comments