0% found this document useful (0 votes)
11 views5 pages

5.user Defined Methods 2024 25

Uploaded by

Jathin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

5.user Defined Methods 2024 25

Uploaded by

Jathin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In Java

Function & advantages


 A program module which can be called at different
instances in a program to perform a specific task.
 Advantages:
 Reuse of code
 Memory optimisation
 Divides complex task into small methods-modular
approach, function can be executed separately
Components
 public static void main(String args[])
public int add(int a,int b,int c)//a,b,c formal parameters
 {//method block
}
 Types: inbuilt, user defined functions
 protected double add(int a,double b)//header or
prototype
 add(int a,double b)-Method signature
 Access specifier, return, method block.
type,name,parameter list
 return statement
 Used at the end of the function- when return is used-
control goes to call
 Returns only one value
 The process of using a method in a program is referred to as calling or
invoking
 Static Methods- we can call the function without the object of the class.
 Non static methods- we need to call the function through the object of class
 Actual parameters-variables in the function call.
 Formal parameters- Variables in the function definition.
 Method -prototype,definition, header means the same.
 Pass by value-copy of actual parameters passed on to formal parameters ,
changes in formal will not affect actual parameters.
 All primitive data types-int,float,double,char will be passed by value by default
 Pass by reference: address/reference of actual parameters passed on to formal
parameters , changes in formal will affect actual parameters as both refer to the
same address.
 All non primitive data types-arrays, objects etc., will be passed by reference by
default.

 Pure method /Accessor: Method which does not change the state of the
object .
 Impure methods/Mutator: Method which changes the state of the object .
Function Overloading
 int area(int l,int b)
 int area(int s)
 double area(double r)
 Methods having the same names but difference in number
of parameters or data types of the parameters are called
over loaded methods.
 We cannot judge overloaded methods or not based on
return types of methods/functions.

 Recursion: function calls itself is called recursive


function.

You might also like