top of page
Search
Writer's pictureSiah Peih Wee

Declaring Functions in Java

Reading function code from a learner with Javascript as their first language might realize its very different. So lets break it down word by word.


First Keyword in Function Declaration

The first keyword is the accessibility of the function throughout the application. And as shown below it means it is set to be accessible throughout the application.

public static int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}

You can also set the accessibility to private or protected. The table below show the meaning of each keywords.

​First Keywords

What it means?

private

Set this function to be accessed only in the class it’s declared in

protected

Set this function may only be accessed in the class (or subclass, or other classes in the same package) it’s declared in.

public

Set this function to be accessible throughout the application.


Second Keyword in Function Declaration

By adding static, means

  1. the function is shared by all instances of a class,

  2. the function does not belong to any instance of the class.

public static int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}

To make it non-static, you can declare it by not adding the keyword static, shown in the following.

public int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}

You may want to read more about what is static



Third Keyword in Function Declaration

The third keyword is to define the data type for function return.

Noticed that the integer variable iSquare is returned, which matches the third keyword declaration.

public static int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}


Name of the Function

The fourth word is to define the name of the function. You can name it name it anyway you like.

public static int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}

However you might want to consider naming conventions such as

  • Upper CamelCase : Each new word begins with a capital letter. Some examples will be like CalculateSquare, StartGame, InitialiseWorld

  • Lower CamelCase : Similar to Upper CamelCase except the first letter is in lowercase. Some examples will be like calculateSquare, startGame, initialiseWorld


Parameters of the Function

The words inside the round brackets are to define the parameters of the function. The first word int inside the bracket is used to define the parameter. The following word iValue is the name given to the incoming integer parameter.


Noticed the name has a "i" before the word Value? This is a Hungarian notation.

public static int Square(int iValue)
{
	int iSquare = iValue * iValue
	return iSquare;
}

You may want to read more on

If you want add more variables, to do this you can write more by adding a commas followed by parameter declaration.

public static int Multiply(int iValueA, int iValueB, int iValueC)
{
	int iMultiply = iValueA * iValueB * iValueC;
	return iMultiply;
}

Function Overloading in Java

Because unlike Javascript, Java does not have the default parameters, therefore you may want to use Overloading instead. Below is an example of overloading

public static int Multiply(int iValueA, int iValueB)
{
	int iMultiply = iValueA * iValueB;
	return iMultiply;
}

public static int Multiply(int iValueA, int iValueB, int iValueC)
{
	int iMultiply = iValueA * iValueB * iValueC;
	return iMultiply;
}

public static int Multiply(int iValueA, int iValueB, int iValueC, int iValueD)
{
	int iMultiply = iValueA * iValueB * iValueC * iValueD;
	return iMultiply;
}

So depending in the number of parameters being passed in, it will run the relevant function.

You may want to read more on

191 views0 comments

Comments


Post: Blog2 Post
bottom of page