Functions in Java - ProgrammerTech
Language:

Search

Functions in Java

  • Share this:
Functions in Java

The concept of functions in Java

A function is a set of commands that perform a specific task and are called when needed from anywhere in the program. I mean, if we have a code and we want to write it more than once, for example, a code that combines two numbers and we need to write it more than once. Once or ten times, and in this case, we will face the problem of repeating the code and wasting effort and time. Instead of repeating the code, we put the code inside a function and then call the function when needed. For this code, we have shortened the effort and time. And time and we made our program short, organized and beautifully. The function is built before the main program, ie in the body of the class or at the bottom of the main program. And many prefer to write it below the name of the class above the main program.

functions JAVA

This lesson is very important for any beginner or advanced programming expert to be familiar with this subject and without knowing how. Function construction A person is not called a programmer or understands programming without this topic, which is the construction of functions.

 

Benefits of using functions in Java

  1. Shortcut and reduce code.
  2. Program organization.
  3. Get results faster.

 

Types of functions in Java

We have two types of functions:

 

Return type Functions

Functions that return a value ( Return type ), for example, a function we call and send to it with numbers that add these numbers and then return the sum. Or we give it a name and it searches for the specific character and returns the character we want. We are building these functions with the intent of doing a specific action and the specific action is the return value.

General form of functions that return a value in Java

Type FunctionName(Parmetrs)
{
body //The code we need
    Return type;
}

Where type is the type of the return value, if it is text we write a string and if it is a number we write an int.

And FunctionName is the name of the function and it is an optional name and it is preferable that the name of the function refers to the function of the function.

Parmetrs are also optional. We can write parameters or leave them without parameters. According to the program's need, that is, if we want a function, we send it to it, for example, numbers so that it can add or divide. We write the number of parameters we will receive and then process them inside the function and return the final value.

 

Return not type Functions

Functions that do not return a value ( Return not type ) we use a lot like the previous functions, for example if we want a function that prints a certain sentence that does not return. A value, just print a sentence or send it parameters that process them and store them in the database.

The general form of functions that do not return a value in Java

void FunctionName(Parmetrs)
{
body //The code we need
}

Where void means do not return a value, then the name of the function and the parameters are optional, which we mentioned a while ago.

example 1

Write a function to which we send two numbers, then add the two numbers, and then print the result into the main program.

package funlesson;
public class FunLesson {
    int add(int x, int y)
    {
        int z=x+y;
        return z;
    }     
    public static void main(String[] args) {
        FunLesson s=new FunLesson();
        System.out.println(s.add(5, 2));
    }
}

We call the add function and we will have two parameters and the value returned by the function is an int, the number. The first return will be stored in x and the second number will be stored in y. And we created an object derived from the name of the class because without creating an object the contents of the class cannot be accessed. To call the add function we put the two values ​​sent 4 will be stored in x and 2 will be stored in y. And then we called the function and sent it two values ​​that will add the two values. And return the result and store it in the variable result and print the result.

Example 2

package fun;
public class Fun {
    int add(int x, int y)
    {
        int z=x+y;
        return z;
    }     
    public static void main(String[] args) {
        Fun ob=new Fun(); 
        int result=ob.add(4, 2);    
        System.out.println(result);
    }
}

And we can print directly without defining the variable by telling it to return x+y; And the output will be printed, and we can also write the program in short form, as in the form.

package fun;
public class Fun {
    int add(int x, int y)
    {
        return x+y;
    }
    public static void main(String[] args) {
        Fun ob=new Fun();
        System.out.println(ob.add(4, 2));
    }
}

This is also one of the benefits of functions, which is an abbreviation of the program.

example 3

package fun;
public class Fun {
    void add(int x, int y)
    {
        System.out.println(x+y);
    }    
    public static void main(String[] args) {
        Fun ob=new Fun();
        ob.add(4, 2);
    }
}

This Return not type function does not return an a value.

Example 4

Build two functions, the first that reads the values ​​from the keyboard and stores them in an array, and the second function that collects the elements of the array.

package fun;
public class Fun {
    Scanner n=new Scanner(System.in);
    void read(int a[])
    {
        for(int i=0;i<a.length;i++)
            a[i]=n.nextInt();
    }
    int addArray(int a[])
    {
        int sum=0;
        for(int i=0;i<a.length;i++)
            sum+=a[i];
        return sum;
    }  
    public static void main(String[] args) {
        Fun ob=new Fun();
        int []a=new int[6];
        ob.read(a);
        System.out.println(ob.addArray(a));
    }  
}

We created a one-dimensional array int []a=new int[6] named a and size 6, then we put the input object in the class so that the functions can identify the object. Then we started building the two functions, the first we called it read will receive an argument of type array and then we LOOP the array. And then we store the entered values ​​from the keyboard in the array nextInt and the work of this function is to read the values ​​from the keyboard and then store the values ​​in the array. Then we built the second function and its work is to collect the elements of the array and return the result and its name is addArray. The same array is received and inserted into a lube loop as the LOOP in the first function. Ie when it reaches the size of the matrix stops. And we defined a variable named sum in this variable. The elements of the array will be collected and stored in this variable, and we gave it an initial value of 0. After we have collected the elements of the array, we return the value stored inside the sum and we return it to the array.

Then we went to the main program after we built the functions and called the first function. Via ob.read then we send the array a and then we call the second function by printing its value into the print command ob.addArray. We said at the beginning of the lesson that a function that returns a value either assigns the return value to a variable or puts it inside the print function.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com