USER DEFINED FUNCTIONS IN JAVA
In mathematics, you might have studied about functions. For example, f(x) = x2 is a function that returns squared
value of x.
If x = 2, then f(2) = 4 If x = 3, f(3) = 9 and so on.
Similarly, in programming, a function is a block of code that performs a specific task. A function can also be
referred to as a method.
Types of Java methods
Depending on whether a method is defined by the user, or available in standard library, there are two types of
methods:
Standard Library Methods
User-defined Methods
Standard Library Methods
The standard library methods are built-in methods in Java that are readily available for use.
Eg: sqrt() is a method of Math class. It returns square root of a number.
User-defined Method
You can also define methods inside a class as per your requirements. Such methods are called user-defined
methods.
Let us take an example of how to create a method in Java:
public static void DemoMethod() {
[Link](“This is the first function I created”);
}
Here, a method named DemoMethod() is defined.
You can see three keywords public, static and void before the function name.
The public keyword makes DemoMethod() method public. Public members can be accessed from outside of the
class.
The static keyword denotes that the method can be accessed without creating the object of the class.
The void keyword signifies that the method doesn’t return any value.
The complete syntax for defining a Java method is:
modifier static returnType nameOfMethod (Parameter List) {
// method body
}
modifier - defines access type whether the method is public, private and so on.
static - If you use static keyword in a method then it becomes a static method. Static methods can be called
without creating an instance of a class.
For example, the sqrt() method of standard Math class is static. Hence, we can directly call [Link]() without
creating an instance of Math class.
returnType - A method can return a value like int, float, double etc.
If the method does not return a value, its return type is void.
nameOfMethod - The name of the method is an identifier. You can give any name to a method. However, it is
more conventional to name it after the tasks it performs. For example, calculateInterest(), calculateArea(), and so
on.
Parameters (arguments) - Parameters are the values passed to a method. You can pass any number of
arguments to a method.
Method body - It defines what the method actually does, how the parameters are manipulated with
programming statements and what values are returned. The codes inside curly braces { }forms the body of the
method.
USER DEFINED FUNCTIONS
An example user-defined function called AreaOfRectangle which calculates the
area of a rectangle by accepting two parameters length and breadth .
Output:
USER DEFINED FUNCTIONS IN JAVA
How to call a Java Method/Function?
Now that we know how to define methods, we need to learn to use them. For that, we have to call the method.
Here's how:
DemoMethod();
This statement calls the DemoMethod() method.(defined earlier)
Let us look at the example below:
1. While executing the program code, it encounters myFunction(); in the code.
2. The execution then branches to the myFunction() method and executes code inside the body of the method.
3. After the execution of the method body, the program returns to the original state and executes the next
statement after the method call.
An Example program
class CallDemo {
public static void main() {
[Link]("I will call the method now");
DemoMethod();
[Link]("Now I am back to the main method");
}
private static void DemoMethod(){
[Link]("See I was called!");
}
}
Output:
I will call the method now
See I was called!
Now I am back to the main method
In the above program, we have a method named DemoMethod(). The method doesn't accept any arguments. Also,
the return type of the method is void (means doesn't return any value).
Here, the method is static. Hence we have called the method without creating an object of the class.
Method Return Value and the return statement
Methods may return a value to the calling function. Return is a reserved keyword in Java. It is used
to exit from a method, with or without a value. The return keyword is used to return from a method when
its execution is complete.
You declare a method's return type in its method declaration. Within the body of the method, you use
the return statement to return the value. Any method declared void doesn't return a value.
Let us consider the following examples:
Program 1 Program 2
class ReturnDemo class ReturnDemo
{ {
public static void add() public static int add()
{ {
int a=3,b=4; int a=3,b=4;
int c=a+b; int c=a+b;
[Link]("Sum is "+c); return c;
} }
public static void main() public static void main()
{ {
add(); int d=add();
} [Link]("Sum is "+d);
} }
}
Output: Output:
Sum is 7 Sum is 7
In Program 1 the method add() does not return a value, so its return type is void.
In Program 2 the method add()returns a value, so its return type is int.
Let's take another example of a method returning a value:
class SquareDemo {
public static void main() {
int result;
result = square();
[Link]("Squared value of 10 is: " + result);
}
public static int square() {
return 10 * 10;
}
}
Output:
Squared value of 10 is: 100