This document provides information on user defined methods in Java. It defines what a method is, which is a sequence of statements grouped together and given a name that can be called to perform a specific task. Methods are used to simplify program complexity, hide details, enable code reuse, and simplify maintenance. The document discusses method declaration, which includes the header and body. The header specifies the return type, method name, and parameters. Methods can be called by passing arguments. The document contrasts call by value versus call by reference and discusses recursive methods and method overloading.
2. What is method?
A method or a
function is a sequence
of statements
grouped together and
given a name
This group of statements
can be called at any point in
the program using its name
to perform a specific task.
3. Why to use
methods?
Methods are used
in the programs for
various reason
To code complexity
To hide the details
For reusable code
To simplify program
maintenance
5. Method Declaration
It declare either
specifies the type of
the value which a
method will return
or
use the keyword void
to indicate that it will
not return any value
6. Method Declaration
A method consists of
two parts:
• Header part
• Body part.
The method header is the
first line of the function
declaration or definition.
The method body contains a
set of statements for related
operations written in curly
brackets.
7. Method Declaration
Header part :
1. Modifier
2. Return type
3. Method name
4. Parameter list
Modifier tells the compiler how to
call the method.
Return type specifies the type of
value returned from a function.
Function name is the name
assigned to the method.
Parameter list is comma-
separated list of variables of a
function.
8. The methods are of
two types:
1. Built in method
2. User defined
method
The built-in methods are
pre-defined methods
stored in the Java library.
The user-defined methods
are defined by the
programmers as per their
need.
9. A Method exists in
three different forms
within a program:
Method prototype
Method definition
Method call
10. Method Prototype
Method Definition
Function prototype is the first line of the method
definition ended by semicolon that tells the
program about the signatures of the function.
A method is called (invokedorexecuted)by
providing the method name along with the
argument list enclosed in the parenthesis.
11. Method Call
A method is called (invokedorexecuted)by
providing the method name along with the
argument list enclosed in the parenthesis.
12. Method Signature
It is basically refers to
the number and types
of the arguments in the
method.
It itself is used to refer
to a method prototype.
Public class drawing
{
Public void draw(String s)
{….}
Public void draw(int i)
{….}
Public void draw(double d)
{….}
Public void draw(int I, double s)
{….}
}
Is also called method overloaded
13. Arguments
An argumentis a value itself
that is passed to a method when
it is executed
Parameters
the parameter list appears
between the parentheses
following the method name.
Public void main(){
A=obj.Sum(5,8);//5,8 arearguments being
passed toparameter x
andy
----------
----------
}
Float Sum(int x, int y){
Float add=(x+y)/2; //x andy
areparameter
Return add;
}
14. Actual parameter
Theparameter that appear in a
method call statement arecalled
actual parameter.
Formal Parameter
the parameter that appear in the
function definition are called
formal parameter.
Public int sum(int a, int b)
{
return a+b; //a andbareformal parameter
}
int length=10;
int breadth=5;
int add=sum(length,breadth);
//areactual parameter
15. How to access
Method?
A method can be
accessed or called by
providing the name of
the method followed
by parameter enclosed
in parentheses.
Class callmethod
{
Public double area(double a, double b)
{
double x;
x=a*b;
return x;
}
Public void accessingmethod()
{double x,y,z;
x=3.0;y=9.3;
z=area(x,y);//calling method area()
System.out.println(z);
16. Pure functions
Thefunction which returnvalues
and do not change state are
called purefunction.
impure function
Thefunction which changesthe
state of objects is called impure
function
class add
{
private double a,b,c;
public add()
{a=10.0;b=10.0;c=1.0;}
public double sum()
{
return(a+b);// purefunction, return exact value
}
public void sum1()
{c=a+b;//impurefunction, value ofcvaries
System.out.println(c);}
17. Passing values to
method
Values can be passed
in the following two
ways,
1. Call by value
2. Call by address
(reference)
Call by value
Themethod createsits newset of variables to copy the
value of actual parameters andworks with them
Call by address
Reference of the actual parameters is passed on to the
method. No newset ofvariables is created.
18. Call by value
class passbyvalue
{
public void changed()
{
int a=12;
System.out.println(“original value=“+a);
System.out.println(“changed
value=“+value(a));
System.out.println(“again value=“+a);
}
public static int value(int x)
{
x=10;
return x;
}
}
19. Call by Reference
class Test
{
int x;
Test(int i) { x = i; }
Test() { x = 0; }
}
class Main {
public static void main(String[] args) {
Test t = new Test(5); // t is a reference
change(t);
System.out.println(t.x);
}
public static void change(Test t)
{
t = new Test();
t.x = 10;
}
}
20. RecursiveMethod
Recursionin java is a
process in which a method
calls itself continuously.
It makes the code compact
but complex to understand.
public class RecursionExample1 {
static void p(){
System.out.println("hello");
p();
}
public static void main(String[] args) {
p();
}
}
21. Method overloading
Method overloading
means that there are
more than one
function doing
different kinds of jobs
but have same name.
this process is also
known as
polymorphism.
Public class calc
{
Public void sum(int s,int v)
{System.out.println(s+v);
}
Public void sum(int I, int j,int k)
{
System.out.println(i+j+k);
}
Public static void main(string args[])
{
calc ob=new calc();
Ob.sum(7,8);
Ob.sum(4,5,7);}
}