0% found this document useful (0 votes)
2 views

Functions

Uploaded by

gamingperfect20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Functions

Uploaded by

gamingperfect20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Functions/methods

• Syntax: • Eg;
<access specifier><return type><method name>(parameter list)
public int Add(int m,int n)
{
{
statements;
int s=0;
----------------
s= m+n;
-----------------
return(s);
return()value;
}
}
Function

public int Add(int m,int n) // header of the method or function signature

{
int s=0; Method block
s= m+n; (body of the method)
return(s);
}
Program using function
import java.io.*;
class Sum
{
int Add(int m,int n)
{
int s=0;
s=m+n;
return(s);
}
public void main() throws IOException
{
int a,b,p;
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter two numbers”);
a=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
Sum ob=new Sum(); Creating object of class Sum
p=ob.Add(a,b); Calling of method Add()
System.out.println(“The sum of two numbers=”+p);
}
• Write a program in java using a class with the
following specifications:
class : sum
Data members/instance variables : int a , b, c
Member functions:
void getdata():to accept the numbers a and b
void calculate(): to find the sum of two numbers
void display():to display the sum
Write the main function to call these functions.
import java.io.*;
class sum
{
int a,b,c;
void getdata()throws IOException
{
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter two numbers”);
a=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
}
void calculate()
{
c=a+b;
}
void display()
{
System.out.println(“Sum of the numbers”+c);
}
public void main()
{
sum ob= new sum();
ob.getdata();
ob.calculate();
ob.display();
}
}
• Code a program in java using a class with the
following specifications:
Class name : Pay
Data members/instant variables: float salary,da,hra,gross
Member functions:
void inputdata() : to accept name and salary of an employee
void calculate() : to find the following
da= 15% of salary
hra= 10% of salary
gross = salary + da+hra
void outputdata() : to display all the information.
Function overloading
• Definition:
A function name having several definitions in the same
scope that are differentiable by the number or types of
their arguments ,is said to be an overloaded
function.Process of creating overloaded functions is called
function overloading.
• In otherwords, it is the system of defining
functions/methods with the same function names but with
different number and types of parameters.Such functions
are called overloaded functions.
• Polymorphism is the way of declaring functions with the
same name but for different purposes.
• System finds the best match of the function arguments and
the parameter list (ie;types and number of parameters)
during program compilation.This phenomenon is termed as
Early binding or static binding.
Need for function overloading
• If different function names are used for similar
operations then users may have lot of
interactions and they require to remember a
number of function names at the time of
invocations.Hence, it is reliable to use many
functions with the same names for similar
operations.
Function overloading not only implements
polymorphism but also reduces number of
comparisons in a program and thereby makes the
program run faster.
Overloaded functions
• Eg; Invalid functions:
 int area(int a,int b);  int area(int a,int b);
 double area(double a,double b);  int area(int x,int y);
 double area(int a,double b);
 double area(double a,double b);
 public int area(int a,int b) ;
 public int area(int a,int b,int c);

A function argument list is known as the


function signature.For overloaded
functions ,the functions should have
same function name but with different
signatures.
Program to find the area of circle,square,rectangle
class calculate
{
double area(double r)
{
double ar1=22/7*r*r;
return(ar1);
}
int area (int s)
{
int ar2=s*s;
return(ar2);
}
double area(double a,double b)
{
double ar3=a*b;
return(ar3);
}
public void main()
{
int n;
double m,n,p;
calculate ob= new calculate();
m=ob.area(5.3);
n=ob.area(8);
p=ob.area(6.2,3.6);
System.out.println(“area of ciircle”+m);
System.out.println(“area of square”+n);
System.out.println(“area of rectangle”+p);
}

You might also like