Functions in Java
A function is named unit of program codes that can be called from anywhere in the class. The primary purpose of using functions is to save ourselves from duplicating our code.
Anatomy of a Function
The figure above explains the anatomy of a function
public is the access specifier i.e. the scope of availability of this function.
The modifier static is optional and its presence in function declaration means that the function can be called without making an object of its class.
void is the return type and it means that the function does not return any value. If a function is to return a value then void should be replaced with the data type of the returned value.
myFunc is the function name by which the function is identified. It can be any valid identifier in java. If you follow the convention, which you must, then you should begin the name of the function with a lowercase letter.
Function prototype – It is the first line of a function definition that informs the compiler about the number and type of arguments it will receive and the type of value it will return.
Return statement is used in a function to transfer back the control or to return a value to the calling part (function).
Function signature – It is a part of the function prototype and refers to the number and type of arguments a function receives.
function body is the place where the program code is placed.
Need for Functions
- Code Reusability Using functions saves a lot of time. In situations where we have a few lines of code that we want to use again and again then instead of writing the same code at several places we can put it inside a function. Now this function can be called everytime we need to perform the same steps.
- Avoiding ambiguity There comes a time when you find yourself writing hundreds of lines of code only to end up frustated when you need to debug your code. Putting chunks of code in functions helps in organizing your code in a better way and thus avoiding ambiguity.
- Compulsory in OOP In Object Oriented Programming (OOP) languages such as Java, you need to declare at least one function i.e. main. Apart from that, as you start writing your own classes you’ll find it very useful to use methods(functions declared inside class is known as a method) that assigns(Setter method) or returns(Getter method) values.
Types of functions in Java
- Computational functions – Functions that calculate some value and return the result. Ex:- Math.sqrt() etc.
- Manipulative functions – Functions that indicate success or failure of an operation by returning 0 or 1.
- Procedural Functions – Functions that simply perform an action and do not return a value. Ex:- System.out.print().
Call by Value (Pass by Value)
In this method of function invoking, the values of actual parameters are copied into formal parameters and any changes made to the formal parameters is not reflected back to the actual parameter.
Call by Reference (Pass by Reference)
In this method of function invoking, the address of the actual parameter is copied into formal parameter and any change made to the formal parameter is reflected back to the actual parameter.
The function which does not modify the state of the object/data received as parameters is known as a pure function.
The function which modifies the state of the object/data received as parameters is known as an impure function.