Java Tutorial
Functions in Java
Introduction
Functions or methods are an important part of programming in any language as they allow a pre-defined piece of code to be executed from any part of the program. They also become important when we look at classes in a later page.
A function is basicly a piece of predefined code that can be called from anywhere within the program. To make them more useful hey can be passed variables, access public variables already in the program and return values. In the programs you have written so far you have only had one function or method – method main. This is the line
public static void Main(String[] args)For now you don't need to know what all that means but just to know that it defines a method called main(which is called every time you run the program). The code that is between the {} is the code that is executed every time this method is called.
Defining a method/function
To define a method you only need to things, the name of the method and the return type. The name is simply what its called and can petty much anything you like. The return type is the type of variable that the method returns to the program after it has been executed.You can also declare a method as public or private(like you can with variables) this becomes important when you write multiple class programs as they decide which parts of the program can access them. For the moment you should declare everything as public as it means everything can access them, the default is private if you leave this part of the decoration out
Anyway lets look at an example:
import java.io.*;
public class functions
{
int x, ans;
public static void main(String[] args)
{
x = 4; //give x a value, you can change this value to see the different results you get
ans = square(x); //call function square
System.out.println(x + “ squared is: “ + ans);
}
public int square(int num) //function sqaure defined here
{
//code for the function goes here
int numsqrd;
numsqrd =num * num;
return numsqrd //returns a number
}
}
This example has a function called square, this simply squares any given number and returns the result as an integer.
The main program calls this in the line
“ans = square(x);”
This sets the value of ans to the return value of sqaure. This is done using the line
“return numsqrd”
The number you want to square is passed to it in brackets and is assigned to variable in the brackets of the functions definition, the “(int num)” part. This works like a normal variable deceleration and multiple variables can be declared here, separated by commas (,)
Any code that you want the function to perform can be put within the {} brackets eg the lines
//code for the function goes here int numsqrd; numsqrd =num * num;These lines can also access any variables declared at the start of the program and change their values etc. In fact you may want to make a function that only does this and doesn't return a variable, ill show you how to do this further on.
In the line
public int square(int num);
the int parts tells the program the return type at the end. This can be any normall data type as well any objects such as a String. You can also use the type “void” if you dont want the function to reurn a value, if you do this please ensure there isnt a return command at the end. If you do put a return type you MUST return a vlaue of that type in evrey way the fuction can be executed.
Calling a Function/Method
You can call a method or function in 2 ways.
If the function has a return type you can have variable = function(). Please note that you can also include a function in calling another function eg variable = function1(function2(), function3()). This would pass function 1 the results of function2 and function 3, it would have the same affect as:
variable2 = function2();
variable3 = function3();
variable = function1(variable2, variable3).
If a function doesn't have a return type you can call it on its own eg function1() would be a valid way of calling function1
bravenet.com