User defined method
Function: Function is a building block which consist of set of statements to perform a particular task and
when we call the function that set of statements are executed.The function can be called anytime, any
number of time, anywhere in the program.functions are also called method or subprograms all
procedure or module.
A function is divided into 2 categories:
1. Predefined or inbuilt functions:
The functions which are predefined by compiler or already present in jdk are known as
predefined functions
2. User defined function:The function which is created by user or programmer to perform a
specific task is known as user defined function.In this table function name of function is given by
user.
Advantages of functions :
• A function heights the details of a program.
• It provides the reusability Of code As a function once defined can be used again and again.
• it makes the program modular that is a big program can be divided into small programs.
Components of Function
A function/ method has following three components:
a) Function prototype/ function declaration
b) Function definition
c) Function calling/ invoking/ executing
Syntax: access_specifier modifier return_type function_name(list of variables)
{
// body of function
a) Function prototype: First line of function definition is called function prototype which gives
following information about function to user:
i) Name of function ii) Return type of function iii) Access
specifier iv) Number of parameters and their data type.
Syntax: access_specifier modifier return_type function_name(list of variables)
Ex: public static void fun(int x) where public and static are optional.
Ex: public int fun(int a , int b)
b) Function definition: It consists set of statements to perform a particular task which are inside
curli braces followed by function prototype.
public void sum(int a, int b)
{ int s;
s= a+b; //function definition
System.out.println(s);
c) Function calling: To execute the statements of function definition is called function calling.
Function can be called by specifying its name followed by valid number of parameters inside the
parenthesis and ends with semicolon.
Ex: function_name();
class ABC
{ Formal parameters
public void sum(int a, int b) // function prototype
{ Function signature
int s=a+b;
System.out.println(“sum=”+s);
} public static void main(String args[])
{
ABC ob=new ABC();
ob.sum(10,15);//function calling
Actual parameters
Output: sum=25
Ex:
class ABC
{ public int sum(int a, int b)
{
int s=a+b;
System.out.println(“sum=”+s);
return s;
} public static void main(String ar[]) {
ABC ob=new ABC();
int r= ob.sum(10,15);
System.out.println(“result=”+r); }
}
Output: sum=25
result= 25
return statement: return statement is used to return a value from function definition to function call.
The data type of the returned valued must be declared in function prototype. If a function doesn’t
return any value then its return-type must be written as void.
Actual parameters: The parameters which are passed in the function calling statement are known as
Actual parameters.
Formal parameters: The variables declared inside the function prototype to receive the values of
actual parameters are known as Formal parameters. Formal parameters must have compatible data
type with actual parameters.
Ways to define and invoke a Function
Parameterized function: The function which receives parameters is called parameterized function. Ex:
fact(int a)
Non Parameterized function: The function which doesn’t receive any parameters is called non
parameterized function. Ex: fact ( ).
Function calling: Function can be called in two ways:
i) Call by value ii) Call by reference
i) Call by value: In call by value, values of actual parameters are copied into formal parameters
and any change in formal parameters doesn’t effect in actual parameters. The parameters are of
primitive data types.
Ex: class swap
{ public void fun(int a, int b)
{ int temp=a;
a=b; b=temp;
System.out.println(a+”,”+b ); // 20, 10
} public static void main(String args[])
{ int x=10,y=20; swap ob=new swap();
ob.fun(x, y);
System.out.println(x+”,”+y); // 10, 20 }
}
Call by reference: In this type of calling actual parameters are passed by reference(address) to formal
parameters. In this type of calling any change in formal parameters will change in actual parameters. In
this type of calling only reference datatype arguments are passed( object, array).
Ex: class swap { int x=10, y=20; public void
fun(swap ob1)
int temp=ob1.x; ob1.x=ob1.y;
ob1.y=temp;
System.out.println(ob1.x+”,”+ob1.y ); // 20, 10
} public static void main(String args[])
{ swap ob=new swap(); ob.fun(ob);
System.out.println(x+”,”+y); // 20, 10 }
Pure function: The function which does not change or modify the formal parameters is called pure
function. It is also called Accessor function.
Impure function: The function which change or modify the state of the formal parameters is called
Impure function. It is also called Mutator function.
Function/Method Overloading: When two or more functions are declared in a class having same name
but with different number (or data types or both) of parameters, is called function overloading. i.e. Two
or more functions having same name but different signature.
Ex: class calc
{
public void area (int l, int b)
{
int ar=l*b;
System.out.println(“area=”+ar);
}
public void area (int s)
{
int as=s*s;
System.out.println(“area=”+as);
}
public void area (int r)
{
double ac=3.14*r*r;;
System.out.println(“area=”+ac);
}
public static void main(String args[])
{
calc ob=new calc();
ob.area(3,4);
ob.area(3);
ob.area(3.5);
}
}
Constructor : They are special type of function which have same name as class name and
doesn’t have any return type not even void. The purpose of constructor is to initialize the data
members of a class.
Constructor can be of two types:
1. Non Parameterized constructor: Constructor without any parameters is called non
parameterized constructor or default constructor. They generally initialize data
members with their default values. If there is no constructor define in class then
compiler automatically define a default constructor at time of object creation.
2. Parameterized Constructor: Constructor defined with parameters to initialize data
members are called Parameterized Constructor .
Constructor Overloading : When there are more than one constructor defined for a class with
difference in number , type or both of parameters then it’s called Constructor Overloading
class Box {
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
// specified
Box() { width = height = depth = 0; }
// compute and return volume
double volume() { return width * height * depth; }
public static void main()
{
Box obj=new Box();
System.out.println(obj.volume());
Box ob2=new Box(2.0,3,4);
System.out.println(ob2.volume());
}
}