A return statement, similar to a method invocation, needs parentheses around the return value: return(3);.


Return statements need () around the return value


Return statements do not need () around the return value

Here is what's right.

The return statement is a special kind of statement, starting with the word return. That word is either followed by an expression that will evaluate to the value to return, or it is directly terminated with a ;:

return 3;

Placing the expression into parentheses is not necessary at all. Worse, it is confusing, because then the return statement looks like a call to a method named “return”, which it is not.

How do you know your students might have this misconception?

The following example shows an occurrence of this misconception where only one of the three return statements uses parentheses around the expression. The return statements with literals don’t have parentheses, but the return statement with a non-atomic expression surrounds that expression with a parenthesis.

public class L {
 public static int f(int x) {
  if (x<0) {
   return 0;                   // no (...)
  } else if (x==0||x==1) {
   return 1;                   // no (...)
  } else {
   return (f(x) + f(x-1) + 1); // extra (...)

A similar example, observed in a control-flow graph drawn by a student, showed a node containing return i but another node containing return (-1). In this case the student might also have wanted to ensure that the minus is seen as a unary operator and not as an operand in a binary subtraction (return - 1).

How can you build on this misconception?

For very advanced students it may make sense to discuss that if one emulates continuation-passing style in Java, one might end up writing code where methods end by calling their continuation, which might look like ret(args).

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.