FUNCTION- Function is a sequence of statements which perform a specific and unique task in a program.
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)
public static int add(int a, int b)
* 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 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-:
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.)
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.