0% found this document useful (0 votes)
31 views5 pages

Function Theory

A function in programming is a block of code that performs a specific task and can be predefined or user-defined. Functions help manage code complexity, promote reusability, and simplify debugging. They can be invoked in various ways, support overloading, and can be categorized based on their return types and arguments.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Function Theory

A function in programming is a block of code that performs a specific task and can be predefined or user-defined. Functions help manage code complexity, promote reusability, and simplify debugging. They can be invoked in various ways, support overloading, and can be categorized based on their return types and arguments.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FUNCTION

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.
function is either pre defined or user defined.

Pre defined function : functions which are already defined in the java libraries is called pre
defined function. For Example Math.pow(), charAt() etc.

User defined function : function which are defined by the user according to their need is
called user defined function.

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.

user defined function have two part -


1 function definition
2 function calling

function definition : A function must be defined before it is used anywhere in the


program.the syntax of function definition is-
[access specifier][modifier] return-type function-name (parameter list)

body of the function

access specifier : can be either Public, Protected or Private.


modifier : can be one of final, native, synchronize, transient, volatile.
return-type : specifies the type of value that the return statement of the function returns. It
may be any valid Java data type(int, double, char, string, void).
Function name : This can be any valid identifier, but it is always advisable to take logical and
meaningful names.
parameter list : is comma separated list of variables of a function.

public static void add(int a, int b)


{

}
access specifier : public
return type : void
function name : add
parameter list : a,b

Function prototype : The first line of a function definition that consists of access specifier,
modifier, return type, function name and list of parameters enclosed within a pair of parenthesis
is called function prototype.
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.
in above example (int a,int b) is function signature.

Function calling : A function can be accessed or invoked by simply writing the name of the
function along with the argument list (if any) otherwise the argument list is kept empty.

class ABCD
{
public static void add(int x, int y) // function definition
{
System.out.println(x+y);
}

public static void main( )


{
int x = 10, y = 20;
add (x, y); // function call
}
}

void keyword : The void keyword indicates that a function does not return any value . Thus a
function that does not return a value is declared as follows. void <functions name> (parameter
list)

Actual Parameters / Arguments : The parameters that appear in function call statement are
called actual parameters.

formal Parameters / Arguments : The parameters that appear in function definition


statement are called formal parameters.

class abc
{
public static void add(int x, int y) // formal parameters
{
System.out.println(x+y);
}
public static void main( )
{
int x = 10, y = 20;
add (x, y); // actual parameters
}
A function is invoked in two manners:

Call by value/Pass by value : In call by value, the called functions creates its own work copy
for the passed parameters and copies the passed values in it. Any changes that take place
remain in the work copy and the original data remains intact.

Call by reference/Pass by reference: It passes a reference to the original variable of the


actual parameters to formal parameter. through this reference, it access the original data.
Any changes that take place are reflected in the original data.

In Java, all primitive data types are passed by value and all the reference types are passed by
reference.

return statement : The return statement is useful in two ways. First an immediately exit from
the function is caused as soon as a return statement is encountered and the control back to the
main caller. Second use of return statement is that it is used to return a value to the calling
code.

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 are called procedural
functions. They generally do not have any return type.
e.g. System.out.println( );

Pure 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.
public static int maximum(int a, int b)  Pure Function
{
int c = Math.max(a,b);
return c;
}

Impure functions : 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 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, type or order 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
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));
}
}

A function be of one of the four types depending on its argument list and return type –
1. Function with no argument & no-return type
Example
class ABCD
{
public static void display( )
{
System.out.println(“Loyola School”);
}
public static void main( )
{
display( );
}

}
2.function with Argument & no-return type
Example
class ABCD
{
public static void display(int x)
{
System.out.println(“The value of x =” + x);
}

public static void main( )


{
int x = 10;
display(x);
}
}
3.function with No-argumentat & with return type
Example
class ABCD
{
public static int display( )
{
int x = 10;
return x;
}
public static void main( )
{
int x = display( );
System.out.println(“The value of x =” + x);
}
}
4. Function with Argument & with return type
Example
class ABCD
{
public static int display(int x)
{
int y = x * x;
return y;
}
public static void main( )
{
int x = 10;
int y = display( x );
System.out.println(“The square value of x =” + y);
}

}
}

You might also like