Module: (Java) Subroutines: Procedures and Functions - 2


Problem

7/11

Boolean function

Theory Click to read/hide

Often programmers use boolean functions that return boolean values ​​true or false (trueor false)
Such functions are useful for   check some property.
Consider two examples of writing a logical function that checks a number for evenness

1)  Better way:
expression result
n % 2 == 0
will be true (True) or false (False)
No need to write a conditional statement!
2) Don't do that!
You can write it like that, but it’s better not to do a longer record anyway
static Boolean isEven(int n)
{
    return (n % 2 == 0);
}
static Boolean isEven(int n)
{
    if (n % 2 == 0)
        return true;
    else
        return false;
}

And the last note about working with functions and procedures: the number of functions and procedures in the program is not limited. In addition, one subroutine can call another subroutine and even itself.
 

Problem

An integer is called prime if it is only divisible by 1 and itself. Write a boolean function (a function that returns true or false) that determines whether a given number is prime.
The main program uses the result of the logic function.