Function In Java

Function/Method

Definition of Function:

A program module used simultaneously at different instances in the program is called function or method .

A function/method is a type of procedure or routine. A function in a programming language is a program fragment that ‘knows’ how to perform a defined task.

A Java function/method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.

In Java, all functiondefinitions must be inside classes. We also call functions methods.

Types of Function:

There are two types of function:

System defines function

User defines function.

System defines Function:

System define function is also known as Built in function.e.g. pow(),sqrt(),main()

User defines Function:

Which functions are created by user is Known as User define function.

How we create user defines function in Java

The following example to explain the syntax of create user defines function:

public static int functionName(int a,int b)

{

// body

}

In Above Syntax:

public static : modifier.

int: return type

functionName: function name

a, b: formal parameters

int a, int b: list of parameters

we know that ,Methods are also known as Procedures or Functions:

Procedures: They don’t return any value.

Functions: They return value.

Method definition consists of a method header and a method body. The same is shown below:

modifier returnType nameOfMethod(ParameterList)

{

// method body

}

The syntax shown above includes:

modifier: It defines the access type of the method and it is optional to use.

returnType: Method may return a value.

nameOfMethod: This is the method name. The method signature consists of the method name and the parameter list.

Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.

method body: The method body defines what the method does with statements.

The void Keyword:

The void keyword allows us to create methods/function which do not return a value. If a function used void keyword then there is no need to return the value to function.

Example.

Function with void keyword

Public void sum()

{

System.out.println(“sum function”);

}

Function without void keyword

Public int sum()

{

System.out.println(“sum function”);

Return 0;

}

How we can pass value to function?

We can pass value to a function using:

1.Call By Value

2.Call by Reference

Call By Value in Java Or Pass By value :-

If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.In pass by value mechanism method is passed the value provided by the caller, therefore, changes made to the formal parameters of the method have no effect on the original values that were passed by the caller

Example of Call By value :-

class callbyval

{

void show(int i,int j)

{

i*=2;

j/=2;

}

}

class callbyValue

{

public static void main(String as[])

{

int a=90;

int b=70;

System.out.println(“before call”);

System.out.println(“a=”+a);

System.out.println(“b=”+b);

callbyval ca=new callbyval();

ca.show(a,b);

System.out.println(“AFTER call”);

System.out.println(“a=”+a);

System.out.println(“b=”+b);

}

}

OUTPUT:

Call By Reference in Java Or Pass By reference :-

When we call a method passing a reference of object, it is known as Call by reference.It is also known as pass by reference. In Call by reference original value is changed if we made changes in the called method. If we pass object in place of any primitive value, original value will be changed. for example:-

class callbyref

{

int a,b;

callbyref(int i,int j)

{

a=i;

b=j;

}

void display(callbyref o) //pass an object

{

o.a*=2;

o.b/=2;

}

}

class callref

{

public static void main(String as[])

{

callbyref c=new callbyref(15,20);

System.out.println(“before call”);

System.out.println(“a=”+c.a);

System.out.println(“b=”+c.b);

c.display(c);

System.out.println(“AFTER call”);

System.out.println(“a=”+c.a);

System.out.println(“b=”+c.b);

} }

OUTPUT:

When we pass an object to a method the situation change dramatically. Because objects are passed by Reference. When we creating a variable of a class type, you are only creating a reference to an object. Change to the object inside the method do effect the object used as an argument. When object reference is passed to a method, the reference itself is passed by use of call-by-value.