|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
|
|
|
|||||||
|
|
Large programs are generally avoided because it is difficult to manage single instructions. Thus, a large program is broken down into smaller units known as functions.
Function: A named unit of a group of program statement. This unit can be involved from other parts of the program. The most important reason to use functions is to make program handling easier as only a small part of the program is dealt with a time thereby avoiding ambiguity. Functions make a program more readable and understandable, thereby making program management easier.
Components of a function
A function is divided into two components a header and a body
Syntax: public int GetMarks() { … … … … … … return marks }
Above, the first line is the function header which specifies the return type, the function name and the parameter-list of the function. It is also known as a function signature.
Function signature consists of:
Types of Functions: Functions can categories into: i. Pure function: A pure function is one that takes objects or primitives as arguments but does not modify the objects.
Example:
class building { String loc; int nfloor; float area; int rfloor; boolean verify;
void liftreq() { if (nfloor>4) { verify = true; System.out.println("the requirement is true"); } else { verify = false; System.out.println("the requirement is false"); } }
public void building(String args[]) { building residential = new building(); building commercial = new building(); residential.loc="salt lake"; residential.nfloor=15; residential.area=7200; residential.rfloor=12; residential.liftreq(); commercial.loc="park street"; commercial.nfloor=3; commercial.area=10000; commercial.rfloor=23; commercial.liftreq(); } }
ii. Impure Functions: The methods that change the state of the object are called impure function.
Example:
public class Banking { String name; int accno; double accbalance; double withdrawal;
public double transaction(double wthdrw , double blnc) { if(wthdrw > blnc) { System.out.println("Withdrawal Value Rs." +wthdrw +") is greater than available balance"); System.out.println("Sorry !, Transaction not Possible "); } else { blnc = blnc - wthdrw; System.out.println("Transaction Successful"); } return blnc; }
public void output(double c) { System.out.println("Available Balance = Rs. "+c); }
public display() { Banking accholder1 = new Banking(); accholder1.name = "Prashanth"; accholder1.accno = 74231; accholder1.accbalance = 126450.67; accholder1.withdrawal = 25500.0; System.out.println("Name :"+accholder1.name); System.out.println("A/C No :"+accholder1.accno); accholder1.accbalance = accholder1.transaction(accholder1.withdrawal ,accholder1.accbalance); accholder1.output(accholder1.accbalance); Banking accholder2 = new Banking(); accholder2.name = "Anantha"; accholder2.accno = 79625; accholder2.accbalance = 14700.12; accholder2.withdrawal = 31716.0;
System.out.println("\nName :"+accholder2.name); System.out.println("A/C No :"+accholder2.accno); accholder2.accbalance = accholder2.transaction(accholder2.withdrawal , accholder2.accbalance); accholder2.output(accholder2.accbalance); } }
Function Declaration: In Java all functions are defined in the body of the class, there are no independent functions. The functions defined inside a class are known as methods. Methods are declared inside the class but immediately after the declaration of instance variables. The general form of a methods declaration is
Syntax: type methodname(parameter list) { method body; }
Method declaration has four basic parts : 1) The name of the method (methodname) 2) The type of the value the method returns (type) 3) A list of parameters (parameter list) 4) The body of the method.
The type specifies the type of value the method would return. This could be a simple data type as int as well as any class type. It could even be void type, if the method does not return any value.
The parameter list is always enclosed in parentheses. This list contains variable names and types of all the values we want to give to the methods as input. The variable in the list are separated by commas. In the case where no input data are required the declaration must retain the empty parentheses.
Example:
(int m,float x, float y) /*three parameters ( )
The body actually describes the operations to be performed on the data. Let us consider the Rectangle class ‘again and add a method getData() to it
Example:
class Rectangle { int length int width void getData(int x,int y) { length = x; width = y; } }
Function Prototype Function prototyping is one useful feature of Java function. A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the number and type of arguments and the type of return values.
Thus; A function prototype is a declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments. The prototype function looks just like a function definition except that it has no body ie. Its code is missing. Function prototyping enables a compiler to carefully compare each use of the function with the prototype to determine whether the function is invoked properly i.e. the number and types of arguments are compared and any wrong number or type of the argument is reported.
Definition: A function prototype is the first line of the function definition that tells the program about the type of the value returned by the function and the number and type of arguments.
Syntax:
Type function.name(parameter list);
Example: Float volume (int a, float b, float c);
The return statement: This statement works like a jump statement. The return statement used to explicitly return from any method. It passes program control back to its caller. The return statement is optional in void methods. We can give return statement any method to come out of method at any step. Return statement has two forms; i. return; ii. return value;
Two important things to remember about return values are: i. The type of data returned by a method must be compatible with the return type specified by method ex: If return type is Boolean you cannot return an int. ii. The variable receiving the value returned by a method must also be compatible with the return type specified for the method ex: In the above example variable x must be of int type.
Passing arguments to functions: A function call can be invoked in two manners: 1) Call by value. 2) Call by reference
In Java simple type data are passed by value and objects are passed by reference.
Call by value When an argument is passed to a function using call by value method, the copy of value of an argument passed to actual parameters of functions. That is changes made to the parameter of the function would not affect the actual argument. The main benefit of call by value method is that you cannot alter the variables that are used to call the function because any change that occurs inside function is on the function’s copy of the argument value. The original copy of the argument value remains intact. Following program illustrates
Example:
public class Call_Value { public void before_change(int x) { x = x * 20; System.out.println("\n Before: x = "+x); after_change(x); System.out.println("\n After: x = "+x); } public int after_change(int x) { x = x + 1000; return x; }
}
Input: ‘x’ value as 10 Output: Before: x = 200 After: x = 200
Call by references: In call by reference we do not pass the values of the variables instead we pass the address of the variables to the calling method. The receiving method does the manipulations on the contents of the memory locations and writes back the results in the respective memory locations.
Example:
public class Call_Ref { int x = 10;
public void Before_Change() { Call_Ref obj = new Call_Ref(); obj.x = obj.x * 10; System.out.println("\n Before : x = "+obj.x); After_Change(obj); System.out.println("\n After : x = "+obj.x); } public int After_Change(Call_Ref inst) { inst.x = inst.x + 1000; return inst.x; } }
Output: Before: x = 100 After: x = 1100
Function Overloading: When several function declarations are specified for a single function name in the same scope, the (function) name is said to be overloaded. Java allows functions to have the same name if it can distinguish them by their number and type of arguments. For example, following two functions are different for java.
Syntax:
float divide(int a , int b) { ……… ……… }
float divide(float x , float y) { ……… ……… }
Above; divide() taking two int arguments is different from divide() taking two float arguments. This is known as function overloading.
Declaring and Definition
The ‘this’ keyword: ‘this’ is used to refer to the current object, which is using the method. The ‘this’ keyboard can be used to the object’s instance variables.
To current to the current object, the keyboard is used where normally one would refer to the object’s name. When there are multiple objects using the same method and all the object’s using instance variables are being accessed then the use of this keyword is essential.
Example: class UseOfThis { int x, y; UsefThis(int a, int b) { this.x = a; this.y = b; } }
|
|
|
|||||
|
|
|
|
|
|||||
|
|
|
|||||||
|
|
||||||||