0% found this document useful (0 votes)
18 views26 pages

Functions

The document provides an extensive overview of functions in programming, detailing their definitions, advantages, parts, and types. It includes examples of function prototypes, definitions, and various programs demonstrating the use of functions for mathematical operations, area, volume, and perimeter calculations. Additionally, it discusses function overloading, parameters, and the differences between various types of functions.

Uploaded by

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

Functions

The document provides an extensive overview of functions in programming, detailing their definitions, advantages, parts, and types. It includes examples of function prototypes, definitions, and various programs demonstrating the use of functions for mathematical operations, area, volume, and perimeter calculations. Additionally, it discusses function overloading, parameters, and the differences between various types of functions.

Uploaded by

codingmaster2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lno .

7 Functions
Function : it is set of instructions given to the computer to perform a specific task.
Each function has a unique name.

Advantages of functions:
1. Program becomes modular
2. Error detection is easy
3.Better Memory Management.

Parts of Functions:
1. Header/Prototype/Signature: it contains the return type, function name and parameter list
2. Body: it contains the instructions that the function will perform.

Eg Math.pow( 3,2) S.length();


Defining a function: psvm( int n )
1. Access specifier: scope of using the functions. Eg public , private
2. static : it enable the function to be called without creating an
object.
3. Return type: it determines the data the function will return after
executing .
return is used at the end of the function.
no statement is executed after return
it returns single value.
when return type is void it means NO RETURN VALUE
4. Function name: it is name given to a function.
5. Formal parameter: it is the value which the function takes from the
user
6. Function body:it contains the instructions that the function will
perform.
1. Write function prototype for function add which takes an int a and string b and returns a character
char add( int a , String b) char add( int a , String b)

2. Write function prototype of function search which takes a double value and a String value and returns true
boolean search( double a , String b)
3. Write function prototype of function read which takes a word w and a sentence s and returns 0.
int read( String w , String s)

4. Write function prototype of function input which takes no value and returns a decimal.
double input()

5. Write function prototype of function poschar which takes a character argument and float argument and returns
no value. void poschar( char a , float b)

Functions are of two types:


1. Predefined : already defined in java Eg pow() , charAt(), length(), sqrt() etc
2. Userdefined : they are defined by the user.
Write a program using functions to add , sub and multiply 2 numbers.
void add( int a, int b)
void sub( int c, int d)
void mult( int e , int f) psvm()
class Fun { Fun obj=new Fun();
{ void add( int a , int b) obj.add(10,20);
{ int sum=a+b; obj.sub(30, 20);
Sopln(“The sum is “+sum); obj.mult(10,20);
} }//main
void sub( int c , int d) }//class
{ int diff=c-d;
Sopln(“The diff is “+diff);
}
void mult( int e , int f)
{ int prod=e*f;
Sopln(“The prod is “+prod);
}
Write a program using functions to find Area of the following .
void circle( int r) 3.14*r*r
void square( int s) s*s
void traingle( int h, int b) 0.5*h*b
psvm()
class Area { Area obj = new Area();
{ void circle( int r) obj.circle(10);
{ double cir=3.14*r*r; obj.square(10);
Sopln(“Area of circle is”+cir); obj.triangle(10,20);
} }//main
void square( int s) }//class
{ int sq=s*s;
Sopln(“Area of square”+sq);
}
void traingle( int h , int b)
{ double tri=0.5*h*b;
Sopln(“Area of triangle is “+tri);
}
Write a program using functions to find volume of the following . class Volume
void cylinder( int r, int h ) 3.14*r*r*h {void cylinder( int r, int h )
void cube( int s) s*s*s { double a=3.14*r*r*h;
void cuboid ( int l, int b, int h) l*b*h Sopln(“vol of cyl is”+ a);
}
void cube( int s)
{ int b=s*s*s ;
Sopln(“vol of cube is”+ b);
}
void cuboid ( int l, int b, int h
{ int c=l*h*b;
Sopln(“vol of cuboid is”+ c);
}
psvm()
{ Volume obj=new Volume();
obj.cylinder( 10,20);
obj.cube(20);
obj.cuboid( 10,20,30) } }
class Peri
Write a program using functions to find perimeter of the following . { void square( int s)
void square( int s ) 4*s { int a=4*s;
void rectangle( int l , int b) 2*(l+b) Sopln(“Peri of sq is”+a);
void circle ( int r ) 2*3.14*r }

void rectangle( int l , int b)


{ int a1=2*(l+b);
Sopln(“Peri of rect is”+a1);
}
void circle ( int r )
{ double b1=2*3.14*r;
Sopln(“Peri of cir is”+b1);
}
psvm()
{ Peri obj=new Peri();
obj.square(10);
obj.rectangle(10,20);
obj.circle(10);}}
Write a program using functions to find Area of the following .
double circle( int r) 3.14*r*r
int square( int s) s*s class Area
double traingle( int h, int b) 0.5*h*b { double circle( int r)
{ double a=3.14*r*r;
return(a);
}

int square( int s)


{ int b= s*s;
return(b);
}

double traingle( int h , int b)


{ double c= 0.5*h*b;
return(c);
}
psvm()
{ Area obj=new Area();
double x=obj.circle(10); Sopln(“ Area of cir is”+x);
int y=obj.square(10); Sopln(“Area of sq “+y);
double z=obj.traingle( 10 ,20); Sopln(“Area of tri “+z); } }
Write a program using functions to find volume of the following .
double cylinder( int r, int h ) 3.14*r*r*h class Volume
double cube( int s) s*s*s {
double cuboid ( int l, int b, int h) l*b*h double cylinder( int r, int h )
{ double a=3.14*r*r*h; return(a);
}
double cube( int s)
{ double b=s*s*s ;
return(b);
}
double cuboid ( int l, int b, int h
{ double c=l*h*b;
return(c);
}
psvm()
{ Volume obj=new Volume();
double x= obj.cylinder( 10,20); Sopln(“Vol of cy”+x);
double y = obj.cube(20); Sopln(“Vol of cub”+y);

double z= obj.cuboid( 10,20,30) ; Sopln(“Vol of cubiod”+z);


}}
Write a program using functions to find perimeter of the following .
void square( int s ) 4*s class Peri
int rectangle( int l , int b) 2*(l+b) { void square( int s)
double circle ( int r ) 2*3.14*r { double a=4*s;
Sopln(“Peri of sq is”+a);
}

int rectangle( int l , int b)


{ int a=2*(l+b);
return(a);
}
double circle ( int r )
{ double b=2*3.14*r;
return(b);
}

psvm()
{ Peri obj=new Peri();
obj.square(10);
int x= obj.rectangle(10,20); Sopln(“Per of rect”+x);
double y= obj.circle(10); sopln(“Peri of cir”+y);
}}
Function overloading : using more than one function in the class with same function name
but different parameter.
WAP using function overloading to find area of the following
void area( int s) area of square(s*s) psvm()
void area( int l , int b) area of rect ( l*b)
void area ( double r) area of circle (3.14*r2) { Area obj=new Area();
obj.area(10.0);
class Area obj.area(10,20);
{ void area( int s) obj.area(10);
{ int x=s*s; }
Sopln(“Area of sq”+x); }
}
void area ( int l , int b)
{ int y=l*b;
Sopln(“Area of rect”+y);
}
void area( double r)
{ double z=3.14*r*r; Sopln(“Area of cir”+z); }
Write a program using functions overloading to find volume of the following .
double cube ( int r, int h ) 3.14*r*r*h class Volume
double cube( int s) s*s*s
double cube ( int l, int b, int h) l*b*h {
double cube( int r, int h )
{ double a=3.14*r*r*h; return(a); }
double cube( int s)
{ double b=s*s*s ;
return(b); }
double cube ( int l, int b, int h )
{ double c=l*h*b;
return(c); }
psvm()
{ Volume obj=new Volume();
double x= obj.cube( 10,20); Sopln(“Vol of cy”+x);
double y = obj.cube(20); Sopln(“Vol of cub”+y);
double z= obj.cube( 10,20,30) ; Sopln(“Vol of cubiod”+z);
}}
Write a program using functions overloading to find perimeter of the following .
double peri( int s ) 4*s class PERI
double peri ( int l , int b) 2*(l+b) { double peri( int s)
double peri ( double r ) 2*3.14*r { double a=4*s; return(a);
}

double peri ( int l , int b)


{ double a=2*(l+b);
return(a);
}
double peri ( double r )
{ double b=2*3.14*r;
return(b);
}

psvm()
{ PERI obj=new PERI();
double z=obj.peri(10); Sopln(“Peri of sq”+z);
double x= obj.peri(10,20); Sopln(“Per of rect”+x);
double y= obj.peri(10.0); sopln(“Peri of cir”+y);
}}
Write a program using functions overloading to find volume of the following .
void check( int n) to check if odd or even
void check( String s) to convert the string to uppercase
void check( int a , int b) to print the greater number.
class Funs
{ void check( int n)
{ if ( n%2==0) psvm()
sopln(“No is even”); { Funs obj=new Funs(); int x=10;
obj.check(x);
else
obj.check(“java”);
sopln(“No is odd”); obj.check( 20, 5);
} }//main
void check( String s) }//class
{ Sopln(“ Upper Case “+s.toUpperCase());
}
void check( int a , int b)
{ if ( a>b )
Sopln(“Greater”+a);
else if( b>a)
Sopln(“ Greater “+b);
else Sopln(“Both Equal”);}
Difference between Actual Parameter and Formal Parameter

Actual Parameter Formal Parameter

1. not preceded by datatype 1. preceded by datatype


2. Used while calling a function 2. used while declaring a function
eg obj.add(x) void add( int x)

Two ways of invoking function : Call by value and call by reference

Call by value call by reference


1. formal parameter receives copy of data 1. formal parameter refer to the memory location
from actual parameter of actual parameter.
2. Change in formal parameter does not affect 2. Change in formal parameter affect
actual parameter actual parameter
3. Primitive datatype is passed as parameter 3. Object is passed as parameter
Difference between pure/accessor function and impure/mutator function

pure/accessor function impure/mutator function


1. It does not change the state of object. 1. It changes the state of object
2. it will return a value 2. it may or may not return a value
eg int add( int x) void add( int x)
{ return(x) ; { x=x+10; Sopn(x); }
}

this keyword:
1. it refers to the current calling object
2. it resolves the name collision between instance and local variables.
Series in Function Overloading
class Overload
{ double series(double n) psvm()
{ double sum=0.0d; { Overload obj= new Overload();
int x=1; double p=obj.series(20.0);
for( int i=0;i<=n;i++) Sopln(“Sum of series”+p);
{ sum=sum+1.0/x; double q=obj.series(2.0,5.0);
x++; Sopln(“Sum of series”+q);
} }//main
return(sum); }//class
}

double series(double a , double n)


{ double sum=0.0d;
int x=1; int y=2;
for( int i=1;i<=n;i++)
{ sum=sum + x/Math.pow(a,y);
x=x+3;
y=y+3;
}
return(sum); }
Design a class to overload a function series () as follows:
(a) void series (int x, int n) – To display the sum of the
series given below:
x1 + x2 + x3 + ……………… xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 ……………. p terms

below: 1/2+1/3+1/4 ................1/10


(c) void series ( ) – To display the sum of the series given
class Overload
void series ()
{
{
void series (int x , int n) double sum=0.0;
{ for (int i=2; i<=10;i++)
double sum=0.0; {
for (int i=1;i<=n;i++) sum=sum+(double)1/i;
}
{
System.out.print("Sum= "+sum);
sum=sum+ Math.pow(x,i); }//end of function
}
System.out.println(sum);
psvm()
}
{ Overload obj = new Overload();
void series (int p) int x=5;
{ obj.series(x);
obj.series(x,x);
for (int i=1; i<=p; i++)
obj.series();}}
System.out.print((i*i*i)-1+”,”);
}
class Q7 {
void SumSeries(int n, double x) { public static void main() {
double sum=0.0d; Q7 obj=new Q7();
int i; int a=5; double y=5.9;
for(i=1;i<=n;i++) obj.SumSeries(x,y);
{ if (i%2==0 ) obj.SumSeries();
sum=sum-x/i; }}}
else
sum=sum+x/i;
}
System.out.println("sum="+sum); }

void SumSeries() {
int s=0, p=1;
for(int i=1;i<=20;i++) {
p=p*i;
s=s+p; }
System.out.println("sum="+s); }
import java.util.*;
class series { case 2 : int s=0;
public static void main() { for(int i=1 ;i<=5 ;i++)
{
System.out.println("1.series_1
2.series_2 "); s=s*10+1 ;
Scanner sc=new Scanner(System.in); System.out.print(s +" " );
System.out.println("enter your choice"); }
int opt=sc.nextInt(); break;
switch(opt) {
default :System.out.println("invalid
case 1: double sum=0.0d;
int x=2; choice"); }}}
for (int i=1;i<=20;i++)
{
if (i%2==0)
sum =sum-Math.pow(x,i);
else
sum =sum+Math.pow(x,i);
}
System.out.println("sum is ="+sum);
break;

You might also like