Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Module 6: Methods

Methods can be used to define reusable code and organize programming logic. A method definition consists of a name, parameters, return type, and body. There are two ways to call a method depending on its return type - either in a statement for void methods or as a value for methods that return data. Parameters allow methods to work with different input values, and overloading methods enables multiple methods with the same name but different parameters.

Uploaded by

Dexter Ranalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Module 6: Methods

Methods can be used to define reusable code and organize programming logic. A method definition consists of a name, parameters, return type, and body. There are two ways to call a method depending on its return type - either in a statement for void methods or as a value for methods that return data. Parameters allow methods to work with different input values, and overloading methods enables multiple methods with the same name but different parameters.

Uploaded by

Dexter Ranalan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Module 6: Methods

Introduction
• Methods can be used to define reusable code
and organize and simplify coding.
• A method is a collection of statements
grouped together to perform an operation. In
earlier chapters you have used predefined
methods such as System.out.println,
System.exit, Math.pow, and Math.random.
These methods are defined in the Java library.
Defining a Method
• A method definition consists of its method
name, parameters, return value type, and
body.
• The syntax for defining a method is as follows:
Defining a Method (cont.)
• Let’s look at a method defined to find the
larger between two integers. This method,
named max, has two int parameters, num1
and num2, the larger of which is returned by
the method.
• Figure 6.1 illustrates the components of this
method.
Defining a Method (cont.)

FIGURE 6.1 - A method definition consists of a method


header and a method body.
Defining a Method (cont.)
• The method header specifies the modifiers,
return value type, method name, and
parameters of the method. The static modifier
is used for all the methods in this chapter.
• A method may return a value. The
returnValueType is the data type of the value
the method returns. Some methods perform
desired operations without returning a value.
In this case, the returnValueType is the
keyword void.
Defining a Method (cont.)
• If a method returns a value, it is called a
value-returning method; otherwise it is called
a void method.
• The variables defined in the method header
are known as formal parameters or simply
parameters.
Defining a Method (cont.)
• A parameter is like a placeholder: when a
method is invoked, you pass a value to the
parameter.
• This value is referred to as an actual
parameter or argument. The parameter list
refers to the method’s type, order, and
number of the parameters.
• The method name and the parameter list
together constitute the method signature.
Defining a Method (cont.)
• The method body contains a collection of
statements that implement the method.
• In order for a value-returning method to
return a result, a return statement using the
keyword return is required. The method
terminates when a return statement is
executed.
Calling a Method
• Calling a method executes the code in the
method.
• In a method definition, you define what the
method is to do. To execute the method, you
have to call or invoke it. There are two ways
to call a method, depending on whether the
method returns a value or not.
Calling a Method (cont.)
• If a method returns a value, a call to the
method is usually treated as a value. For
example,
– int larger = max(3, 4);
• calls max(3, 4) and assigns the result of the
method to the variable larger.
Calling a Method (cont.)
• Another example of a call that is treated as a
value is
– System.out.println(max(3, 4));
• which prints the return value of the method
call max(3, 4).
• If a method returns void, a call to the method
must be a statement.
Calling a Method (cont.)
void Method Example
• A void method does not return a value.
• The preceding section gives an example of a
value-returning method. This section shows
how to define and invoke a void method.
Listing 6.2 gives a program that defines a
method named printGrade and invokes it to
print the grade for a given score.
void Method Example (cont.)
Passing Arguments by Values
• The arguments are passed by value to
parameters when invoking a method.
• The power of a method is its ability to work
with parameters. You can use println to print
any string and max to find the maximum of
any two int values
Passing Arguments by Values
• When calling a method, you need to provide
arguments, which must be given in the same
order as their respective parameters in the
method signature. This is known as parameter
order association. For example, the following
method prints a message n times:
Modularizing Code
• Modularizing makes the code easy to maintain
and debug and enables the code to be reused.
• Methods can be used to reduce redundant
code and enable code reuse. Methods can
also be used to modularize code and improve
the quality of the program.
Overloading Methods
• Overloading methods enables you to define
the methods with the same name as long as
their signatures are different.
• The max method that was used earlier works
only with the int data type. But what if you
need to determine which of two floating-point
numbers has the maximum value?
Overloading Methods
• The solution is to create another method with
the same name but different parameters, as
shown in the following code:
The Scope of Variables
• The scope of a variable is the part of the program
where the variable can be referenced.
• A variable defined inside a method is referred to
as a local variable. The scope of a local variable
starts from its declaration and continues to the
end of the block that contains the variable. A
local variable must be declared and assigned a
value before it can be used.
The Scope of Variables (cont.)
• A parameter is actually a local variable.
• The scope of a method parameter covers the
entire method. A variable declared in the initial-
action part of a for-loop header has its scope in
the entire loop.
• However, a variable declared inside a for-loop
body has its scope limited in the loop body from
its declaration to the end of the block that
contains the variable, as shown in Figure 6.5.
The Scope of Variables (cont.)
The Scope of Variables (cont.)

You might also like