Functions

Functions in Java
A function is a block or portion of code within a larger program which consists of some declarations and executable statements, which performs a specific task and is relatively independent of the remaining code.
The term method is used alternatively with function and means the same in Java programming language. A larger block of code is broken down into smaller and simpler modules called ‘member methods’ this is how the OOP language Java implements ‘Modularity’.
Advantages of using Functions –
1. To cope with the complexity of code by breaking down complex bigger code into smaller and simpler ones.
2. Hiding details (thus implementing Abstraction)
3. Reusability of Code
4. Makes debugging of errors easy.
Anatomy of a function
FUNCTION NAME ( )
{
Body of the function…
}
Let us now see all the components individually –
1. These are keywords which define the accessibility of the function, if nothing is mentioned then by default it is ‘friendly’ friendly is not a keyword. They are of 4 types
a. Public
b. private
c. protected
d. friendly this is not a keyword and hence cannot be written explicitly.
2. These are keywords which redefines the functionality within the methods. They can be final, native, static, synchronized, transient, volatile etc…
3. They specify what type of value a function can return, hence they can be of any primitive of reference data types like int, byte, float, char, String or even class types. In case the function doesn’t return any value the keyword ‘void’ is used.
4. Function name – This can be any valid identifier, but it is always advisable to take logical and meaningful names, it should conventionally begin with a lowercase letter and in case of multiple words join the words and begin each word in Uppercase except for the first word.
e.g. toUpperCase( )
5. This is a comma separated list of variables along with associated data types. This list can also be empty which indicates the function is non–argumentative.
e.g.
public static void add(int a, int b)
{
…
}
Function prototype
The first line of a function that consists of access specifier, modifier, return type, function name and list of parameters enclosed within a pair of parenthesis is called function prototype.
A function prototype describes the function interface to the compiler, by giving details of number and type of arguments, return type etc…
e.g.
public static void compute(int a, int b)
{
…
…
}
Function signature
The function signature is a part of function prototype; it basically refers to the argument list i.e. number, order and type of arguments.
e.g.
public static void compute(int a, int b)
{
…
…
}
Understanding the keywords public & private
In a function the access specifier indicate the scope of visibility of that particular function, this means whether the data members and member methods are accessible only within the class, or other classes within the same package or other classes in other packages.
Public members are accessible everywhere, within the same class as well as inside other classes that inherits the base class or by creating objects of the former class in the latter one even if they do not inherit, but private members are accessible only within the same class.
Understanding the keyword static
A static data member is also known as a class variable, such variables have only one instance inside the class and we can not create multiple instances through multiple objects.
On the other hand if a member is non-static then such members are called instance variables, which mean we can have multiple instances of such variables through multiple objects.
e.g.
class Exmp1
{
int x; // non-static or instance variable which can have multiple instances
static int y; // static variable or class variable which has only one instance
public static void main( )
{
Exmp1 obj1 = new Exmp1( );
Exmp1 obj2 = new Exmp1( ); // three different objects through which we can
Exmp1 obj3 = new Exmp1( ); // have three different values of ‘x’
obj1.x=10;
obj2.x=20; // the same variable ‘x’ has three different values simultaneously
obj3.x=30;
y = 100; // the variable ‘y’ being static is accessed directly and can have only
// one value
obj1.y=100;
obj2.y=200; // even if we try to access the variable ‘y’ through different objects
obj3.y=300; // the same ‘y’ gets overwritten and finally the last value prevails
}}
So, we see that if a member is static in nature it will have only one copy in the physical memory of the computer, which can be accessed directly without the need of creating objects. But if it is non-static then we need to create objects to specify which instance we are referring to as it has multiple occurrences.
How to access a function?
A function can be accessed or invoked from other functions by simply writing the name of the function along with the argument list (if any) otherwise the argument list is kept empty.
e.g.
class ABCD
{
public static void main( )
{
int x = 10, y = 20;
add (x, y);  function call
}
public static void add(int x, int y)
{
System.out.println(x+y);
}
}
Actual & Formal Parameters / Arguments
The actual parameters are those that appear at the point of function invocation or function call are called Actual parameters.
The formal parameters are those that appear at the point of function declaration or function signature are called Formal parameters.
e.g.
public static void main( )
{
int x = 10, y = 20;
add (x, y);  actual parameters
}
public static void add(int x, int y)  formal parameters
{
System.out.println(x+y);
}
A function be of one of the four types depending on its argument list and return type –
1. Non-argumentative & non-return type
Example
class ABCD
{
public static void main( )
{
display( );
}
public static void display( )
{
System.out.println(“Loyola School”);
}
}
2. Argumentative & non-return type
Example
class ABCD
{
public static void main( )
{
int x = 10;
display(x);
}
public static void display(int x)
{
System.out.println(“The value of x =” + x);
}
} // Note that a function which is void cannot return any value
3. Non-argumentative & return type
Example
class ABCD
{
public static void main( )
{
int x = display( );
System.out.println(“The value of x =” + x);
}
public static int display( )
{
int x = 10;
return x;
}
} // Note that the return type of the function and the variable associated
// with the keyword return must be identical, also note that the function
// at the point of invocation gets equated with a similar datatype.
4. Argumentative & return type
Example
class ABCD
{
public static void main( )
{
int x = 10;
int y = display( x );
System.out.println(“The square value of x =” + y);
}
public static int display(int x)
{
int y = x * x;
return y;
}
}
The return statement
A function terminates as soon as the last statement of the function is encountered or the return keyword is encountered.
It is not necessary that return keyword should be used only in functions that has a return type, even functions that are void can use the return keyword to terminate the function and shift the program control to the calling function but make sure that the return statement does not have anything preceding the keyword ‘return’.
e.g.
class ABCD
{
public static void main( )
{
call( );
}
public static void call( )
{
System.out.println(“Inside the method call”);
return ; // return statement has nothing following it
}
}
Three types of functions:
1. Computational functions – The functions that computes or calculates certain values or quantities and returns the answer to the calling method is called Computational function.
e.g. Math.pow(a,b);
2. Manipulative functions – The functions which manipulates the data and returns information to the calling function as success or failure code in the form of 0 & 1 or true & false.
e.g. Character.isLetter( )
3. Procedural Functions – The functions that perform certain tasks like reading from source files, reading from terminals, displaying data to the terminal are called procedural functions. They generally do not have any return type.
e.g. System.out.println( );
Pure & Impure functions
A pure function is one that takes objects and/or primitive data as arguments but does not modify the objects hence the return value of such functions are either primitive data or an entirely new object created inside the function. They are also known as Accessor methods.
An impure function on the other hand modifies the state of its objects or arguments. They are also known as Modifier methods.
public static int maximum(int a, int b)  Pure Function
{
int c = Math.max(a,b);
return c;
}
public static int product(int a)  Impure Function
{
a = a * a;
return a;
}
Function Overloading
A function name having several definitions in the same scope that are differentiated on the basis of function signature i.e. number of arguments, order of arguments, type of arguments is said to be an overloaded function and the process of creating such functions is called Function Overloading.
Function overloading implements Polymorphism.
e.g.
class Overload
{
public static void area(int s)
{
System.out.println(“Area of square =” + (s*s));
}
public static void area(int l, int b)
{
System.out.println(“Area of rectangle =” + (l*b));
}
public static void area(double r)
{
System.out.println(“Area of circle =” + (3.1415 * r * r));
}
}

Comments