Simple User-defined Methods
Explore how to define and call simple user-defined methods in Java. Understand the method structure including headers and bodies, learn execution sequences, and practice creating reusable code modules to improve programming organization and efficiency.
Methods
We can divide a program into procedural components or modules called methods in Java. We are already familiar with the main() method. Methods can be copied and reused in a programming technique called a modular or procedural approach, also known as the divide and conquer approach. We should always try to design methods that can be reused in other programs too.
Broadly, there are two types of methods, which we’ll explore in the following sections.
The structure of a method
The following slides illustrate various parts of a method definition:
A function name can only contain letters (
A—Zanda—z) and the underscore symbol (_). It may also contain digits (0–9), but the function name can’t start with those. For example,Key_2is a valid function name, but2_keyis not. Function names are case-sensitive, meaning thatname,Name, andNAMEare three different functions.
There are two main parts of a method definition:
- Header: The first line, which ends with a closing parenthesis
). - Body: The block of statements below the method header, starting with an opening brace
{and ending with a closing brace}.
The method header contains the keywords static and void (void means that the method does not have a return value), the method name sayHello, and the parameters in (). All the statements in the method body are written as a block of code enclosed in { and }. We can not write the body of the method without braces.
Method call
The following program demonstrates the creation of a method and a method call.
We can call a method by writing the method name, followed by () and ending with ;. In line 11, we tell the program to call the method sayHello(). This line is known as a method call or calling the method.
Execution sheet for method calls
Let’s look at the following code before working on its execution sheet:
In the program:
- We define two simple methods—the
firstandsecond. - We demonstrate the sequence of execution with the help of
System.out.println()statements.
The most important column to understand the sequence of execution is the PS# column. The sequence of PS# for the above program is .
Here’s another code to help us understand the execution sequence of method calls. The sequence of PS# for the following program is .
The execution sequence can also be visualized as follows:
The illustration above shows the PS# and the depth of each method call more clearly. The first two statements are from the main program, the next three are from the method deep(), and the next one from the method deeper(). The last one is again from the main program. The main program is the caller of the deep() method. The deep() method is the caller of deeper().
Practice creating and calling methods
Here are a few example programs to practice creating and calling methods. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure that the output of your solution matches the given solution. There can be several ways of writing correct solutions in programming.
Method to create a multiplication table
Write a method showTableOf4() that displays 20 terms from the table of 4. Call your method from the main() function to display the results.
Sample output
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48
4 x 13 = 52
4 x 14 = 56
4 x 15 = 60
4 x 16 = 64
4 x 17 = 68
4 x 18 = 72
4 x 19 = 76
4 x 20 = 80