FUNCTION

FUNCTION- Function is a sequence of statements which perform a specific and unique task in a program.
    OR
A function is a block of code under a single name and performs a specific whenever it is invoked by function call statements.

*Function reduces complexity and reduces the length of code and promotes simplicity.

The Syntax for Functions is-:
ACCESS SPECIFIER  MODIFIER  RETURN TYPE  FUNCTION NAME  (LIST OF PARAMETERS)
{
_____________
_____________
_____________
}
e.g.-
public  static  int  add(int a, int b)
{
int c=a+b;
return c;
}
*  In the above syntax, access specifier, modifier and list of parameters is optional to put    in function prototype. But return type and function name is compulsory to be defined.

Let us now learn some important definations-:

FUNCTION PROTOTYPE-:It is the first line of function defination which tells us about the scope of the function,about the type of function,the value returned by the function,about the name of the function,about the number of parameters passed in the function. Function body along with function prototype is called FUNCTION DEFINATION.

FUNCTION SIGNATURE-:It is the number of parameters passed in the function.

ACTUAL PARAMETERS-:They are the parameters which appear at the function calling statements.

FORMAL PARAMETERS-:They are the parameters that appear at the function prototype.

There are two types of functions-:
1-PARAMETERIZED FUNCTIONS- These are type of functions containing some parameters are declared in function prototype.
For ex- void check(int a);
2-NON-PARAMETERIZED FUNCTIONS- These are type of functions containing no parameters in their prototype.
For ex- void check();

Let us practice some questions based on function on Java-:

Q-WAP TO CHECK IF A NUMBER IS AUTOMORPHIC OR NOT?
Q-WAP TO CHECK IF A NUMBER IS MAGIC OR NOT?
Q-WAP TO COUNT THE NUMBER OF VOWELS PRESENT IN A STRING?


POLYMORPHISM:
Polymorphism is a way in which two classes or functions respond to the same message in different ways. It is the process through which the same message can be processed in different ways depending upon the data they act upon. For ex-: If the message of adding two values is given to two different functions having different data types, they will produce different results. like ADD "5"+"4"=54 but ADD 5+4=9; Here the first one is adding of String values and the second one is adding of integer values. Polymorphism is of two types-:
 
1-FUNCTION OVERLOADING
2-CONSTRUCTOR OVERLOADING


CONSTRUCTOR OVERLOADING:
When two or more constructors are made with the same name as that of the class but with different signatures then they are called constructor overloading.(We will discuss more about it in constructor chapter.)


FUNCTION OVERLOADING:
When two or more functions are made with the same name but different signatures and return type then they are called overloaded functions and the process of overloading function are called function overloading. 
Here are some program dealing with this concept-:
Q-WAP to overload a void function "over" which will perform the following functions-:
A- ADD two numbers
B- Factorial of a number
C- Find the largest number among 3 numbers.
Q-WAP TO OVERLOAD A STRING AND BOOLEAN FUNCTION "check" TO PERFORM THE FOLLOWING TASK-:
A-REVERSE A STRING.
B-SEARCH A CHARACTER IN STRING.
Advertisements

Q-WAP TO COUNT THE NUMBER OF VOWELS PRESENT IN A STRING?

import java.io.*;
class vowel2
{
static void vowel(String str)
{
String v="aeiouAEIOU";
int c=0;
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(v.indexOf(ch)!=-1)
c=c+1;
}
System.out.println("TOTAL NUMBER OF VOWELS ARE "+c);
}

public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(newInputStreamReader(System.in));
String str="";
System.out.println("enter str");
str=br.readLine();
vowel(str);
}
}

Q-WAP TO CHECK IF A NUMBER IS MAGIC OR NOT?

import java.io.*;
class mag
{
static boolean mag(int n)
{
int s=0,a=0;
while(n>9)
{
s=0;
while(n>0)
{
a=n%10;
s=s+a;
n=n/10;
}
n=s;
}
if(n==1)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter n");
int n=Integer.parseInt(br.readLine());
boolean f=mag(n);
if(f==true)
{
System.out.println("Magic");
}
else
{
System.out.println("Not Magic");
}
}
}

Q-WAP TO CHECK IF A NUMBER IS AUTOMORPHIC OR NOT?

import java.io.; 
class auto
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=0,num=0;
System.out.println("enter any number"); n=Integer.parseInt(br.readLine());
auto.automorphic(n);
}
public static void automorphic(int n)
{
int num=n,p=1;
while(n>0)
{
n=n/10;
p=p10;
}
if((num*num)%p==num)
{
System.out.println("automorphic");
}
else
{
System.out.println("not automorphic");
}
}
}

Q-WAP TO OVERLOAD A STRING AND BOOLEAN FUNCTION “check” TO PERFORM THE FOLLOWING TASK-: A-REVERSE A STRING. B-SEARCH A CHARACTER IN STRING.

import java.util.*;
class overload
{
public String check(String str)
{
String t="";
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
t=ch+t
}
return t;
}
public boolean check(String str,char ch)
{
int k=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==ch)
{
k=1;
break;
}
else
k=0;
}
if(k==1)
return true;
else
return false;
}
public static void main(String args[])
{
String str="";char ch;String r="";boolean t;
Scanner ob=new Scanner(System.in);
overload obj=new overload();
System.out.println("ENTER STR");
str=ob.nextLine();
System.out.println("ENTER CHARACTER");
ch=ob.next().charAt(0);
r=obj.check(str);
System.out.println(r);
t=obj.check(str,ch);
if(t==true)
System.out.println("PRESENT");
else
System.out.println("NOT PRESENT");
}
}




1-WAP to overload an integer function “over” which will perform the following functions-: A- ADD two numbers B- Factorial of a number C- Find the largest number amon​g 3 numbers.

import java.util.*;
class overload
{
public void over(int a,int b)
{
int c=0;
c=a+b;
System.out.println(c);
}
public void over(int c)
{
int f=1;
for(int i=1;i<=c;i++)
f=f*i;
System.out.println(f);
}
public void over(int d,int e,int f)
{
if(d>e)
System.out.println(d);
else if(e>f)
System.out.println(e);
else if(f>d)
System.out.println(f);
}
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
int a=0,b=0,c=0,d=0,e=0,f=0;
System.out.println("ENTER A");
a=ob.nextInt();
System.out.println("ENTER B");
b=ob.nextInt();
System.out.println("ENTER C");
c=ob.nextInt();
System.out.println("ENTER D");
d=ob.nextInt();
System.out.println("ENTER E");
e=ob.nextInt();
System.out.println("ENTER F");
f=ob.nextInt();
overload obj=new overload();
obj.over(a,b);
obj.over(c);
obj.over(d,e,f);
}
}
/*TO OVOID MULTIPLE INPUT OF NUMBER USING SCANNER CLASS, YOU CAN ALSO USE DIRECT INPUT BY JUST WRITTING THE NAMES OF VARIABLE IN THE MAIN LINE ISTEAD OF String args[]*/