What is Methods(Function) in Java? – Day 8

Published by Maneet Srivastav on

Meaning of the term Method/Function –

Method is a part of a class, and contains a particular set of instructions. In this, you will see how a class can contain not just a main method, but many other methods as well. Normally a method will perform a single well-defined task. Examples of task that a method could perform are calculating the area of a circle, displaying a particular message on the screen, converting a temperature from Fahrenheit to Celsius, and many more. In this you will see how we can collect the instructions for performing these sorts of tasks together in a method.

Outcomes

By the end of this you should be able to:

  • explain the meaning of the term method;
  • declare and define methods;
  • call a method;
  • explain the meaning of the terms actual parameters and formal parameters;

You will also see how, once we have written a method, we can get it to perform its task within a program. When we do this we say that we are calling the methods. When we call a method, what we are actually doing is telling the program to jump to a new place (where the method instructions are stored.) Carry out the set of instructions that it finds there, and, when it has finished (that is, when the method has terminated), return and carry on where it left off.

So in this, you will learn how to write a method within a program, how to call a method from another part of the program and how to send information into a method and get information back.

Declare and Define a Method –

Classes usually consist of two things: instance variables and methods. The topic of methods is a large one because Java gives them so much power and flexibility. However, there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.This is the general form of a method:
<access_specifier> <modifier> <return_type> <method_name>(list_of_parameter) {
// body of method
}

Method in Java - Javatpoint

Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a
value, its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other
items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that
receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.

For Example :

class BrighterBees {
public void display() {
System.out.println(“Hey !!!”);
}
public static void display_two() {
System.out.println(“Welcome to BrighterBees”);
System.out.println(“Enjoy Learning”);
}
public static void main() {
BrighterBees obj = new BrighterBees();
obj.display();
display_two();
}
}

Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:
return value;
In the next few sections, you will see how to create various types of
methods, including those that take parameters and those that return values.

Body of the Method –

The body of this method, which is contained between the two curly brackets, contains the instructions that we want the method to perform, namely to display two lines of text on the screen. The first line, which declares the method, is called the method header, and consists of three words—let’s look into each of these a bit more closely:

  • static
    You have seen this word in front of the main method many times. For now, all you need to know is that functions that have been declared as static (such as main) can only call other methods in the class if they too are static. So, if we did not declare display as static and tried to call it from the main method then our program would not compile.
  • void
    These method simply displays a message on the screen, so we don’t require it to send back any information when it terminates. The word void indicates that the method does not return any information.
  • display()/display_two()
    This is the name that we have chosen to give our method. You can see that the name is followed by a pair of empty brackets. Very soon you will learn that it is possible to send some information into a method—for example, some values that the method needs in order to perform a calculation. When we need to do that we list, in these brackets, the types of data that we are going to send in; here, however, as the method is doing nothing more that displaying a message on the screen we do not have to send in any data, and the brackets are left empty.

Calling a Method –

The idea is that we get the method to perform its instructions as and when we need it to do so—you have seen that this process is referred to as calling the method. To call a method in Java, we simply use its name, along with the following brackets.Java Methods (With Examples)
Using the above example, You can see that the method itself is defined separately before the main method although it could have come after it, since the order in which methods are presented doesn’t matter to the compiler. When the program is run, however, it always starts with main.
We should emphasize again here that when one method calls another method,
  1. the first method effectively pauses at that point
  2. the program then carries out the instructions in the called method
  3. when it has finished doing this, it returns to the original method, which then resumes.

It is perfectly possible for any method to call another method indeed, the called method could in turn call yet another method. This would result in a number of methods being “chained”. When each method terminates, the control of the program would return to the method that called it.

Pass Value to a Method(Function) –

In general, there are two ways that a computer language can pass an parameter to a method. The first way is call-by-value. This approach copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an argument can be passed is call-by-reference.

In this approach, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the method, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine. As you will see, although Java uses call-by-value to pass all arguments, the precise effect differs between whether a primitive type or a reference type is passed. When you pass a primitive type to a method, it is pass by value. Thus, a copy of the argument is made, and what occurs to the parameter that receives the argument has no effect outside the method.

Actual and Formal Parameter –

Beginning Java - Unit 5 Methods - What is a method?

Actual Parameters : Programs pass information using parameters. The variables or expressions referenced in the parameter list of a program call are actual parameters. For example, int s = sum(x,y);

Formal Parameters : Formal parameters are the variables declared in a method specification and referenced in the program body. For example, static int sum(int a, int b).

Example,

class BrighterBees {
static int sum(int a, int b) //Statement 1 {
return a+b;
}
public static void main() {
int x=10,y=20;
int s = sum(x,y); //Statement 2
System.out.println(“Sum of “+x+” and “+y+” : “+s);
)

In Statement 1, the variables a and b are term as formal parameter; whereas in statement 2 the arguments x and y are term as actual parameter (as they are the actual ones to hold the values).

CONNECT WITH THE AUTHOR ON LINKEDIN.
VISIT OUR WEBSITE BRIGHTERBEES FOR INFORMATION ABOUT JAVA.
TO GET ON THE BLOG OF DAY 7 OF THIS JAVA SERIES click here.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US