Java built-in Subroutines and Functions

Java functions definition must be inside the classes. Classes have inside static member subroutines, as well as constant member variables.
 
For example, the System class contains a subroutine named to exit. In a program, of course, this
subroutine must be referred to as System.exit. Calling such subroutine will end the
program.
 
And You may have used it if you have some reason to terminate the program before the end
of the main routine.
 
Likewise, Here integer will be as a parameter, so the whole subroutine call statement will have looks like “System.exit(0);” or “System.exit(1);”.
 
(The parameter here instructs computer why the program had to be terminated. When parameter value equals zero
shows that the program finished normally.
 
Similarly, Any different values show that the program was terminated because an error was detected. But in practice, the value of the parameter is usually
ignored.)
 

Java Functions:

 
Every subroutine is doing some specific tasks. For some of such subroutines, that task is to calculate or provide some data value. They are called Java functions. 
 
So, we usually say that a Java function returns a value. The returned value must then be used somewhere in the program.
 
And, You probably already know the mathematical function that calculates the square root of a number. Java has a corresponding function – Math.sqrt.
 
Likewise, This Java function is a static member subroutine of the class named Math. If x is some numeric, then Math.sqrt(x) calculates and returns the square root of that numeric.
 
Since Math.sqrt(x) provides value, there is no sense to put it on a program line by itself in a subroutine program call statement such as Math.sqrt(x); // This doesn’t make sense!
 
So what would the PC do with the value calculated by the function in this case?
 
And You have to instruct the PC to do something with them that value. And You might, for example, tell the PC to display/print it: System.out.print( Math.sqrt(x) ); // Shows on monitor the square root of x.
or you might use some sort of assignment statement to instruct the PC to store that value in some variable:

 

length of side = Math.sqrt(x);
Call of Math.sqrt(x) represents some value of type double, so where there is a need for such it could be used there.
This class contains in itself many static member functions.

Here is a big list of some of the most frequent usage of them:


• Math.abs(x), which calculates the absolute value of x.

• Our usual trigonometric mathematical functions, Math tan(x), Math sin(x) and Math cos(x).

• The inverse trigonometric functions arcsin, arccos, and arctan: Math atan(x), Math asin(x), and Math acos(x). (also all in radians)

• And the Math.exp(x) for computing the number e raised to the power x, and Math log(x) for computing the logarithm of x in the base e (however I am not still sure where e is set there)

• Math.pow(x,y) for calculating x raised to the power of y.

• And Math.floor(x), which rounds x down to it’s the nearest integer value that is less than or equal to x.

Nevertheless, the return value is mathematically an integer, it is returned as a value of type double, rather than of type int as you might expect. For example, Math.floor(4.86) is 4.0. Math.round(x) returns the closest integer to x. So in that case 4.86 will be 5 and not 4.

• Math.random() – returns a randomly double in the range 0.0 and 1.0. (PC actually computes so-called “pseudorandom” numbers, which are not exactly random but are random enough for most cases.)
For these functions, the type of the parameter—the x or y inside the parentheses—can be
any value of any numeric type.

For most of the functions, the value returned will be double type. However, for Math.abs(x), the value returned will be the same type as x; if x is of type int, then so is Math.abs(x).

So, for example, while Math.sqrt(25) is the double value 5.0, Math.abs(25) is the int value 25.
Take note that Math.random() does not have any parameters. You still need that parenthesis, even if there is nothing between them.

The parentheses let the PC know that this is a subroutine and not a variable. Another example of a subroutine that has no parameters is the
function System.currentTimeMillis(), from the System class.

When this is performed by PC, it gets you the current moment of time, showed as the number of milliseconds that have passed since the somewhat set period of time (the start of the year 1970 in Greenwich Mean Time, if you care). One a millisecond is one-thousandth of a second.

The return value of System.currentTimeMillis() is of type long.

This Java function can be used to calculate the time that it takes the computer to perform a task. Difference between start and end in other words.

Sample Program:

Here is a sample program that doing a few mathematical tasks and reports the time
that it takes for the program to run. On some PC’s, this time might be zero or close to it,
because it is too small to measure.

/**
* This program performs some mathematical calculations and displays
* the results. It also reports the number of seconds that the
* PC spent on this task.
*/
public class TimedComputation {
public static void main(String[] args) {
long startTime; // Starting time of program, in milliseconds.
long endTime; // 
double time; // Time difference, in seconds.
startTime = System.currentTimeMillis();
double width, height, h; // sides of a triangle
width = 35.0;
height = 10.0;
h = Math.sqrt( width*width + height*height );
System.out.print(“A triangle with cathetus 35 and 10 has hypotenuse “);
System.out.println(h);
System.out.println(“\nMathematically, sin(a)*sin(a) + “
+ “cos(a)*cos(a) minus one should be zero.”);
System.out.println(“Let’s check this for x = 2:”);
System.out.print(” sin(2)*sin(2) + cos(2)*cos(2) – 1 is “);
System.out.println( Math.sin(2)*Math.sin(2)
+ Math.cos(2)*Math.cos(2) – 1 );
System.out.println(“(There can be round-off errors when”
+ ” computing with real numbers!)”);
System.out.print(“\nHere is a random number: “);
System.out.println( Math.random() );
endTime = System.currentTimeMillis();
time = (endTime – startTime) / 1000.0;
System.out.print(“\nRun time in seconds was: “);
System.out.println(time);
} // end main()
} // end class TimedComputation