# Example Functions

The general syntax for any Java function can be summarized as:

<output> <functionName> ( <inputs> ) {
}

The next elements are identified:

• Output It specifies the type of data that the function will return. The most common type will be double or double [], that is, real numbers or arrays of real numbers.
• functionName Name that defines the function. This name is used to invoke the function.
• inputs Arguments required for invocating the function. Any type of argument is allowed, for example "()" for empty arguments; "(int n)" for an integer argument; or "(double start, double end, int samples)" for a real range defined by its boundary values and the number of samples."

Multiple functions are included in the following:

Function examples:

 // Calculate the square of 'a'
double square(double a){
return a*a;
}
 // Calculate the modulus of vector (x,y,z)
double modulusVector(double x, double y, double z){
return Math.sqrt(x*x + y*y + z*z);
}
 // Calculate if 'a' is prime
int prime(int a){
// returns 1 if 'a' is prime, else return 0
for(int i=2 ; i<=(a/2) ; i++) {
if(a % i == 0) {
return 1;
}
}
return 0;
}
 // Calculate the a-th number of the Fibonacci sequence
int fibonacci(int a){
int fib_n = 1;  // fibonacci(0)
int fib_m = 1;  // fibonacci(1)
int fib_i;
if (a < 0) {
return 0;
}
if (a < 2) {
return 1;
}
for(int i=2 ; i<=a ; i++) {
// calculate fibonacci(i)
fib_i = fib_n + fib_m ;
fib_n = fib_m;
fib_m = fib_i;
}
return fib_i;
}
 // Calculate the 'a' factorial (a!)
double factorial(int a){
double acc = 1;
for(int i=1 ; i<=a ; i++) {
acc = acc * i;
}
return acc;
}
 // Calculate the binomial coefficients function
double binomial(int m, int n){
return (factorial(m)) / (factorial(n) * factorial(m-n));
}