Formatting page ...
Function/Method & Function Overloading A function is a named portion of code within a larger program which performs a specific task. It is a sequence of some declaration statements and executable statements. Functions are also known as methods, procedure, subprograms or subroutines. Classification Of Functions Based On Availability Predifined functions - Functions which are available within Java package and can be used by importing the required package. Example: Math class provides predefined functions like pow(),sqrt() etc. Userdefined functions - Functions defined by the programmer within the program used for particular tasks. Following is the syntax to define a function/method: <Access Specifier><Return Type><function name>(list of parameters) { Body of the function return statement } where, Access Specifiers can be public, private or protected. Depending upon the nature of the function any one is used. Return type specifies the type of data retuned by the function. Return type is int if the function returns an int value, float if the function returns a floating point value and so on. Function name specifies the name of the function which is related to the process being done in it. Parameter lists are the variables receiving the values passed from the arguments. It is a sequence of type and identifier pairs separated by commas. Though this is the common method of writing a function, a function definition need not contain arguments always. Similarly, all functions need not return a value. If a function does not return a value, its return type is void. Classification of functions based on return type and arguments: 1.Function which does not have an argument or 2.Function which has an argument but no a return type: While defining ,such a function return type: The function may have single or multiple arguments and return type is void. does not have an argument and the return type is Example: void as the function only performs activities like void add2(int a,int b) computation and printing. { Example: int c=a+b; void add1() { int a=10; int b=20; int c=a+b; System.out.println( Sum = +c); } 3.Function with a return type but no argument: The function which returns a value using the return keyword. The return type of the function while defining the function depends on the type of data the function is returning. It can be int, short, System.out.print(c); } 4.Functions which have both argument and return type: Example: int add2(int a,intb) { int c=a+b; return c; 1
|