A function is a reusable portion of a program, sometimes called a procedure or subroutine.
f(x) = 2x + 5In mathematics, the symbol 'x' is a placeholder, and when you run the function for a value, you "plug in" the value in place of x. Consider the following equation, which we then simplify:
y = f(10) // must evaluate f(10) y = 2 * 10 + 5 // plug in 10 for x y = 20 + 5 y = 25 // so f(10) gives the answer 25In programming, we would say that the call f(10) returns the value 25.
methodName(argumentList)where the argumentList is a comma-separated list of arguments (data being sent into the method). Use the call anywhere that the returned answer would make sense.
className.methodName(argumentList) // for static methods objectName.methodName(argumentList) // for instance methods
double x = 9.0, y = 16.0, z;
z = Math.sqrt(36.0); // sqrt returns 6.0 (gets stored in z)
z = Math.sqrt(x); // sqrt returns 3.0 (gets stored in z)
z = Math.sqrt(x + y); // sqrt returns 5.0 (gets stored in z)
System.out.print(Math.sqrt(100.0)); // sqrt returns 10.0, which gets printed
System.out.print(Math.sqrt(49)); // because of automatic type conversion rules
// we can send an int where a double is expected
// this call returns 7.0
// in this last one, Math.sqrt(625.0) returns 25.0, which gets sent as the
// argument to the outer sqrt call. This one returns 5.0, which gets
// printed
System.out.print( Math.sqrt(Math.sqrt(625.0) ) );
Try this code here
import static java.lang.Math.sqrt;
import static java.lang.Math.*; // import all static methods from Math
modifier(s) returnType methodName(parameter list) // this is the signature
{
// method body (i.e. what it does, how it works) -- the definition
}
return expression;
public static int sum(int x, int y, int z)
// add the three parameters together and return the result
{
int answer;
answer = x + y + z;
return answer;
}
public static double average (double a, double b, double c)
// add the parameters, divide by 3, and return the result
{
return (a + b + c) / 3.0;
}
boolean InOrder(int x, int y, int z)
// answers yes/no to the question "are these parameters in order,
// smallest to largest?" Returns true for yes, false for no.
{
if (x <= y && y <= z)
return true;
else
return false;
}
double average(double x, y, z) { } // Each parameter must list a type
printData(int x) { } // missing return type
int double Task(int x) { } // only one return type allowed!
char getALetter() // no parameters void printQuotient(int x, int y) // void return type void killSomeTime() // both
static int myMethod(int x, int y)
{
x = x * 2;
System.out.println("x = " + x);
y = y * 2;
System.out.println("y = " + y);
return x + y;
}
int a = 5, b = 8, ans;
ans = myMethod(a, b);
System.out.println("ans = " + ans);
System.out.println("a = " + a);
System.out.println("b = " + b);
x = 10 y = 16 ans = 26 a = 5 b = 8
int process(double num) { } // method 1
int process(char letter) { } // method 2
int process(double num, int position) { } // method 3
x = process(3.45, 12); // invokes the third function above
x = process('f'); // invokes the second function
double sum(int x, double y); double sum(double x, int y);
System.out.print("The sum is " + sum(3, 4));