Search code examples
pythonfunctionsyntaxexpressioncall

Is a function call an expression in python?


According to this answer, a function call is a statement, but in the course I'm following in Coursera they say a function call is an expression.

So this is my guess: a function call does something, that's why it's a statement, but after it's called it evaluates and passes a value, which makes it also an expression.

Is a function call an expression?


Solution

  • A call is an expression; it is listed in the Expressions reference documentation.

    If it was a statement, you could not use it as part of an expression; statements can contain expressions, but not the other way around.

    As an example, return expression is a statement; it uses expressions to determine it's behaviour; the result of the expression is what the current function returns. You can use a call as part of that expression:

    return some_function()
    

    You cannot, however, use return as part of a call:

    some_function(return)
    

    That would be a syntax error.

    It is the return that 'does something'; it ends the function and returns the result of the expression. It's not the expression itself that makes the function return.

    If a python call was not an expression, you'd never be able to mix calls and other expression atoms into more complex expressions.