5.3 Methods that Return Values
5.3 Methods that Return Values
In the previous sections all the methods had the word void to start their headings. This indicates that the method will do something, but it won't give you a value back. Some languages call this type of method a procedure. In this section on methods will come up with some kind of an answer and will give this value back to where the method call was made. Some languages would call this type of method a function. The way that we make this kind of method call is a bit different from the way we made method calls in the previous two sections.
Suppose we wanted to write a method that would find the mean of 3 numbers. We could write a program like this (assuming that Input.pde was already loaded in a tab).
float mean(int a, int b, int c)
{
return (a + b + c) / 3.0;
}
void setup()
{
int mark1, mark2, mark3;
mark1 = getInt("Enter mark on first test");
mark2 = getInt("Enter mark on second test");
mark3 = getInt("Enter mark on third test");
println("Your average was " + mean(mark1, mark2, mark3));
if (mean(mark1, mark2, mark3) > 50)
println("Passing average");
else
println("Failing average");
float ave = mean(75, 80, 85);
println("The mean of 75, 80 and 85 is " + ave);
}
|
|
This method will calculate the mean of the three parameters. The result of that calculation will be a float. To indicate the return type we put that before the name of the method. To return that result we use the keyword return. After return you put the value you want returned. It needs to be the type indicated in the heading (or one that can be widened to that type).
Note the various ways the method was called. We don't simply call the method as we've done before. We should do something with the returned value. In the first call we print the result of the method. In the second we compare the returned value with another value. In the last call we store the returned value in a variable.
Suppose we wanted to write a method that would determine whether or not a number was an even number. This method should return true if the number is even and false otherwise. We could write the method like this:
boolean isEven(int a)
{
boolean result;
if (a % 2 == 0)
result = true;
else
result = false;
return result;
}
|
|
It is possible to have more than one return statement in a method. So we could rewrite this method without using the variable result.
boolean isEven(int a)
{
boolean result;
if (a % 2 == 0)
return true;
else
return false;
}
|
|
Some people would argue that you should not have more than 1 return statement in a method. It can certainly make your code harder to read if you have too many return statements. You also must make certain that you always return a value. Sometimes the compiler might complain even if you have covered all the possibilites. Consider this method:
boolean isEven(int a)
{
boolean result;
if (a % 2 == 0)
return true;
else if (a % 2 != 0)
return false;
}
|
|
This version has both return statements protected by if statements. Even though those are the only 2 possibilities the compiler is afraid that you might not return something. The previous example avoids that problem because with the else you are guaranteed to return something. Of course the method could have been shortened to use only 1 return statement like this:
boolean isEven(int a)
{
return a % 2 == 0;
}
|
|
Exercise 5.3
- Study this method and answer the questions that follow it.
int mystery(float a, float b)
{
int value = 0;
if (a < b)
value = -1;
if (a > b)
value = 1;
return value;
}
- What is the identifier of the method?
- What are its parameters?
- What type of value if returned by the method?
- What part of the definition forms the heading?
- Rewrite the method using a nested if structure.
- Rewrite the method using multiple return statement.
- The following method definitions lack both punctuation and indentation. Rewrite each definition correcting these defects and state, in a few words, the purpose of each method.
char first(char a char b){if(a<b)
return a else return b}
float second(float a float b)
{float answer if(a<b)answer=a-b else answer=b-a
return answer}
- Consider the following method:
float f(float x)
{
float y;
if (x < 0)
y = x * x * x;
else
y = x * x;
return y;
}
State, with reasons, which of the following fragments are invalid.
println(f(-7));
float x = f(-7);
x = println(f(-7));
float x = -7; f(x);
- Write a method largest that returns the value of the largest of its three float parameters.
- Write a method gcd that returns the value of the greatest common divisor of its two int parameters.
| |