Function in Java
Skip to main content

Function in Java

 


function in java, function in javascript, function in java 8, function in java program, function in java example, call by value and call by reference in java, difference between call by value and call by reference







Function in Java :-

         This tutorial is explain about What is java function and also explain about difference between call by value and call by reference Java and explanation and given example of Java Programming.  


Function:- function can be defind as set of statements that do a particular task.

Two type of functions use in java-

1. Pre defind Funtion:- this type of function is already defind in Java.

For example:- CharAt();

2. User Defind Function:- this type of function defind by the users and its name and types defind by users.

Syntax:-

Without parameters:-

function()

{

Set of Statements

......................

}

You use modifiers before the functions.

Example:- 

public static void function()

{

Set of Statements

......................

}


Parameters type of function- you pass a parameters in side of methods and you return a value.

data_type function_name( parameter1, parameter2)

{

Set of Statements

......................

return value;

}

Example: int Sum(int a, int b){

int c;

c=a+b;

return c;

}


Most Read:- What is Java Features


Calling Functions:-

1. Call by Value:- When you calling a function by value then you pass a variable or constants into method that is a actual parameters. These parameters are sent as formal parameters. If you any changes made by formal parameters will not reflect to actual perameters.

Example:- 

class Addition{

int Sum(int a, int b){

int c;

c=a+b;

return c;

}

public static void main(String [] args){

int x=2;

int y=3;

int z= Sum(x, y);

System.out.println("sum= "+z);

}

}

Output:- 5

2. Call by References:-When you calling a method by references then you pass  a reference of variables into method. These passing parameters act as pointer variable. Any changes made by formal parameters will reflect to actual parameters.

Example:-


class Car{

String ShowCar(String car []){

return car[1];

}

public static void main(String [] args){

String  mCarname[]={"Swift", "BMW", "KIA"};

String mBestcarname=ShowCar(mCarname);

System.out.println(+mBestcarname);

}

}

Output:- BMW

Post a Comment

0 Comments