A return statement, similar to a method call, needs parentheses around the return value, such as 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 reserved word return. That word is either followed by an expression that will evaluate to the value to return, or the word will stand alone with nothing following it. When return is used on its own with no expression following it, it will leave the current function or method call with None as its return value.

Note: When return is used in a try statement with a finally clause, the finally clause is executed before leaving the function or method.

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 parentheses.

def f(x):
    if x < 0:
        return 0
    elif x == 0 or x == 1:
        return 1
        return (f(x - 1) + 1)

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).

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.