JAVA Constructors and Functions


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:

Description: C:\Users\ARITRO CHAKRABORTY\Pictures\JAVA\function.png

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.
Description: Image result for formal and actual parameters in java
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:
Description: https://lh5.googleusercontent.com/QfdJsag01T2LhJ1QZFlRYF4-9VLSqdz_F_9fvjOGVc8AJbwEXtFnAOK1L9kiy-x6c-s07ybiGcaW3xd-mI3z6hjyZrSYSAhMbmZYhoC1d7W_NJ4MrB-RozVwzKZdTmxuQPd3ArohDGquxn_mag
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
  • Creates an instance of the class
  •  
  • Has no return type, not even void.
  •  
  • Same name as the class name.
  •  
  • Called at the time of object creation.
              FUNCTIONS
  • Groups Java statements.
  •  
  • void or a valid return type.
  •  
  • Any name except the class name.
  •  
  • Called when a function call for the specific method is encountered.  

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