How does Python know where the end of a function is?

Question:

I’m just learning python and confused when a “def” of a function ends?

I see code samples like:

def myfunc(a=4,b=6):
    sum = a + b
    return sum

myfunc()

I know it doesn’t end because of the return (because I’ve seen if statements… if FOO than return BAR, else return FOOBAR). How does Python know this isn’t a recursive function that calls itself? When the function runs does it just keep going through the program until it finds a return? That’d lead to some interesting errors.

Thanks

Asked By: Dan

||

Answers:

white spaces matter. when block is finished, that’s when the function definition is finished.

when function runs, it keeps going until it finishes, or until return or yield statement is encountered. If function finishes without encountering return or yield statements None is returned implicitly.

there is plenty more information in the tutorial.

Answered By: SilentGhost

In Python whitespace is significant. The function ends when the indentation becomes smaller (less).

def f():
    pass # first line
    pass # second line
pass # <-- less indentation, not part of function f.

Note that one-line functions can be written without indentation, on one line:

def f(): pass

And, then there is the use of semi-colons, but this is not recommended:

def f(): pass; pass

The three forms above show how the end of a function is defined syntactically. As for the semantics, in Python there are three ways to exit a function:

  • Using the return statement. This works the same as in any other imperative programming language you may know.

  • Using the yield statement. This means that the function is a generator. Explaining its semantics is beyond the scope of this answer. Have a look at Can somebody explain me the python yield statement?

  • By simply executing the last statement. If there are no more statements and the last statement is not a return statement, then the function exists as if the last statement were return None. That is to say, without an explicit return statement a function returns None. This function returns None:

    def f():
        pass
    

    And so does this one:

    def f():
        42
    
Answered By: Stephan202

Python is white-space sensitive in regard to the indentation. Once the indentation level falls back to the level at which the function is defined, the function has ended.

Answered By: fforw

It uses indentation

 def func():
     funcbody
     if cond: 
         ifbody
     outofif

 outof_func 
Answered By: OscarRyz

To be precise, a block ends when it encounter a non-empty line indented at most the same level with the start. This non empty line is not part of that block
For example, the following print ends two blocks at the same time:

def foo():
    if bar:
        print "bar"

print "baz" # ends the if and foo at the same time

The indentation level is less-than-or-equal to both the def and the if, hence it ends them both.

Lines with no statement, no matter the indentation, does not matter

def foo():
    print "The line below has no indentation"

    print "Still part of foo"

But the statement that marks the end of the block must be indented at the same level as any existing indentation. The following, then, is an error:

def foo():
    print "Still correct"
   print "Error because there is no block at this indentation"

Generally, if you’re used to curly braces language, just indent the code like them and you’ll be fine.

BTW, the “standard” way of indenting is with spaces only, but of course tab only is possible, but please don’t mix them both.

Answered By: RichN

So its the indentation that matters. As other users here have pointed out to you, when the indentation level is at the same point as the def function declaration your function has ended. Keep in mind that you cannot mix tabs and spaces in Python. Most editors provide support for this.

Answered By: Mohan Gulati

Interestingly, if you’re just typing at the python interactive interpreter, you have to follow a function with a blank line. This does not work:

def foo(x):
  return x+1
print "last"

although it is perfectly legal python syntax in a file. There are other syntactic differences when typing to the interpreter too, so beware.

Answered By: GaryO

In my opinion, it’s better to explicitly mark the end of the function by comment

def func():
     # funcbody
     ## end of subroutine func ##

The point is that some subroutine is very long and is not convenient to scroll up the editor to check for which function is ended. In addition, if you use Sublime, you can right click -> Goto Definition and it will automatically jump to the subroutine declaration.

Answered By: kstn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.