SlideShare a Scribd company logo
1 of 22
Class 10
Computer Application
Chapter 3
User Defined Methods
in java
Presented by,
Siva Shankari Rajan,
CPSV
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.
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
Method declaration
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
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.
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.
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.
A Method exists in
three different forms
within a program:
Method prototype
Method definition
Method call
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.
Method Call
A method is called (invokedorexecuted)by
providing the method name along with the
argument list enclosed in the parenthesis.
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
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;
}
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
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);
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);}
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.
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;
}
}
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;
}
}
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();
}
}
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);}
}
Thanks for watching

More Related Content

What's hot

Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or FunctionsKuppusamy P
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaMadishetty Prathibha
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloadinggarishma bhatia
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overridingJavaTportal
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methodsnarmadhakin
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsMahika Tutorials
 

What's hot (20)

07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
 
Python list
Python listPython list
Python list
 
List in Python
List in PythonList in Python
List in Python
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java input
Java inputJava input
Java input
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Array ppt
Array pptArray ppt
Array ppt
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 

Similar to Class 10

EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.Questpond
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 

Similar to Class 10 (20)

EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
C# p8
C# p8C# p8
C# p8
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Functions
FunctionsFunctions
Functions
 
Java execise
Java execiseJava execise
Java execise
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Ch06
Ch06Ch06
Ch06
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
C language 3
C language 3C language 3
C language 3
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Function in c
Function in cFunction in c
Function in c
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 

Recently uploaded

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Class 10

  • 1. Class 10 Computer Application Chapter 3 User Defined Methods in java Presented by, Siva Shankari Rajan, CPSV
  • 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);} }