Page 227 - Computer science 868 Class 12
P. 227
08
METHODS
Learning Objectives
8.1 Built-in Method 8.2 User-Defined Method
8.3 Different Parts of a Method 8.4 Different Types of Methods
8.5 Calling Function 8.6 Defining a Method
8.7 Programs Related to Simple Methods 8.8 Built-in Methods
8.9 Programs using Mathematical Functions 8.10 Programs Using Character Methods
8.11 Function Overloading 8.12 Constructor
8.13 this Operator 8.14 Algorithmic Problem Solving Using Methods
Sometimes, a certain blocks of statements is needed to be executed multiple times as and when required. For
example, in a Calculator program, different jobs such as sum or difference would be required to execute more than
once depending on the need of the user. These types of blocks of codes for the same operations may be written once
and executed many times. Such block of codes that are designed to perform an operation is called a method. The
methods are executed only when they are called. They are also called functions.
Let us see an example.
int i,s=0,n;
System.out.println("Enter n");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%2==0)
{
s=s+i;
}
}
System.out.println("Sum : "+ s);
If the above code is required to be used more than once in a program, then we can enclose this code in a method.
Whenever required, the method is called and the code will be executed.
The syntax for declaring a method is:
<Access specifier> <return_type> <method_name>(parameter_list)
{
//Body of the method
}
225
Methods 225

