Java by Example - methods with and without a return value
Until now, we have dealt with methods (=OOP term for functions) of type void mostly. A void method is basically a block of code that is encapsuled in {} brackets and can be called by it's name, like this:
void functionName()
{
do something
}
Whenever you call funcionName(); within your program - the code inside the function, here: do something (pseudocode), is executed.
The following method increments (adds 1 to) a global variable x:
void incrementNumber()
{
x++;
}
A void method (or function) can also have parameters passed to it, of course, but attention: a variable of a simple data type (int, float, double...) you pass to a method cannot be changed directly by the method! Instead, a temporary copy is created inside the method and the passed variable will be left unchanged!
Only objects are passed by reference (and not by value). Strings and arrays are also objects in Java. For the simple data types there exist wrapper classes, for which class methods must be used to convert to and from the simple data types:
//a new object of the Integer class is created and initialized to 3
Integer a=new Integer(3);
//an int variable is declared and receives the value of a
int b=a.intValue();
//now b has the value 3
With this in mind, we can create a method that changes an Integer variable outside our method directly:
Integer a=new Integer(3);
void incrementNumber(Integer x)
{
int y=x.intValue();
//y is incremented
y++;
//the global variable a gets the new value, by initializing a new object
a=new Integer(y);
}
After calling this function, the value of a is now 4 instead of 3. The function has changed it by reference (by changing an alias of the original variable, that is connected with it). This might sound difficult to understand in the beginning, and you won't need to use it often. I just put it in here, to make this toppic complete.
Notice that you can define several methods with identical names (identifiers), provided they can be distinguished by their type or argument list.
You will often use functions which are not of type void, but of type int, float, boolean or whatever instead. See the following example:
int addNumbers(int x, int y)
{
int z=x+y;
return z;
}
Here, you return the sum of the two passed integer numbers in a function, so you can call it like this: a=addNumbers(4,5); and get a=9 as a result. Instead of defining a new variable, you can just as well return the result of the calculation directly:
int addNumbers(int x, int y)
{
return x+y;
}
Here is another example:
boolean compareNumbers(int x, int y)
{
if(x<y)
return true;
else
return false;
}
This function returns true, if the first argument passed is less than the second one, and false otherwise.
Here's another one:
String testNumber(int x)
{
if(x<100)
return "The number is less than 100";
else
return "The number is greater or equal 100";
}
In the two last examples above, you can just as well omit the word else, because the last return is valid, if the function has not been exited before. Whenever a condition that ends with return is true, the function is exited, so a function can have several "returns".
The following applet demonstrates the subject, see the sourcecode to see what's going on:
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project5F extends Applet
{
boolean x=false;
int a, b, c;
Integer d;
//changing a global variable
void incrementNumber()
{
c++;
}
//passing an argument by reference
//you can use the same method name twice, if arguments differ
void incrementNumber(Integer x)
{
int y=x.intValue();
y++;
d=new Integer(y);
}
int addNumbers(int x, int y)
{
return x+y;
}
boolean compareNumbers(int x, int y)
{
if(x<y)
return true;
else
return false;
}
String testNumber(int x)
{
if(x<100)
return "The number is less than 100";
//here we omit the word "else", it is not necessary
return "The number is greater or equal 100";
}
public void paint(Graphics g)
{
a=4;
b=5;
c=6;
d=new Integer(13);
g.setFont(new Font("Helvetica",Font.PLAIN,16));
g.drawString("int c="+c,110,30);
incrementNumber();
g.drawString("now int c="+c,90,50);
g.drawString("Integer d="+d,90,70);
incrementNumber(d);
g.drawString("now d="+d,100,90);
g.drawString("a="+a+", b="+b,100,110);
int k=addNumbers(a, b);
g.drawString("a+b="+k,110,130);
x=compareNumbers(a,b);
g.drawString(""+x,120,150);
g.drawString(testNumber(a),50,170);
a+=100;
g.drawString(testNumber(a),20,190);
}
}
|
|