FUNCTIONS
A program is a set of instructions given to a
computer. These instructions are specified through functions in Java.
NEED FOR FUNCTIONS:-
1. To deal with programming complexity.
2.
3. To hide low level details.
4.
5. To reuse portions of code.
6.
FUNCTION DEFINITION
It’s necessary to define a function in Java before
using it.
EXAMPLE:
FUNCTION PROTOTYPE AND SIGNATURE
Function prototype is the declaration which gives
information about the function to the compiler. It informs the compiler about
the name of the function, number of parameters passed, while giving call to a
function along with the datatype and sequence of parameters. It also states the
return type of the function.
Function Signature basically refers to the type of
and number of arguments (Parameters).
Both of these entities are depicted in the above
picture.
ACTUAL AND FORMAL PARAMETERS
The parameters that appear in function signature
are called Formal Parameters and the parameters that appear in the function
definition are called Actual Parameters.
RETURNING FROM A FUNCTION
The return statement
The return statement causes the following:-
1. An immediate exit from the function. As soon as this statement is
encountered the control passes back to the operating system.
2.
3. Return a value to the calling code.
4.
A function may have several functions but only one
of them is executed as the function terminates as soon as the return statement
is executed.
EXAMPLE:
CONSTRUCTORS
A Constructor is a function with the same name as
that of the class name. It is used to initialize the objects of that class type
with legal initial values.
EXAMPLE:
class Student
{
Student()
{
Statements
;
}
}
Constructors have only one purpose in life: to
create an instance of the class. Example
Student
s=new Student( );
DIFFERENCE BETWEEN CONSTRUCTORS AND FUNCTIONS
CONSTRUCTORS
|
FUNCTIONS
|
TYPES OF CONSTRUCTORS
NON-PARAMETERIZED CONSTRUCTOR
A constructor that accepts no arguments are called
non parameterized constructors. They are considered as default
constructors.
EXAMPLE:
class student
{
student( )
{
statements;
}
}
PARAMETERIZED CONSTRUCTOR
A constructor which accepts arguments are called
Parameterized constructors. Declaring a parameterized constructor hides the
default constructor.
EXAMPLE:
class student
{
student(int variable1, int
variable 2 )
{
statements;
}
}
No comments:
Post a Comment