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");
}
}
}

FLAME SENSOR(with LED)

FLAME SENSOR
ARDUINO CONNECTION WITH FLAME SENSOR

HOW TO CONNECT IT TO ARDUINO?


Steps to connect the sensor to Arduino-:

1- Connect the VCC pin of the sensor to the 5V pin of Arduino.

2- Connect the AO or DO pins to any of the ANALOG or DIGITAL pins on the Arduino. In this setup I have connected the DO pin of sensor to the 8th DO pin on Arduino.

3-Connect the GND pin of sensor to the GND pin of the Arduino.

HOW TO CODE?

After establishing the connection given below we have to connect the Arduino to any device to type the respective code mentioned below.

 int led_pin = 13 ;// initializing the pin 13 as the led pin
int flame_sensor_pin = 8 ;// initializing pin 8 as the sensor output pin
int flame_pin = HIGH ; // state of sensor
void setup ( )
{
Serial.begin ( 9600 );// setting baud rate at 9600
pinMode ( led_pin , OUTPUT ); // declaring led pin as output pin
pinMode ( flame_sensor_pin , INPUT ); // declaring sensor pin as input pin for Arduino
}
void loop ( ) {
flame_pin = digitalRead ( flame_sensor_pin ) ; // reading from the sensor
if (flame_pin == LOW ) // applying condition
{
Serial.println ( " FLAME , FLAME , FLAME " ) ;
digitalWrite ( led_pin , HIGH ) ;// if state is high, then turn high the led
}
else
{
Serial.println ( " no flame " ) ;
digitalWrite ( led_pin , LOW ) ; // otherwise turn it low
}
}
DONT FORGET TO LIKE AND SUBSCRIBE AND PLS DO COMMENT YOUR SUGGESTIONS BELOW…