Print and return statements: the order matters!


I've written this code to find the sum.

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(s):
    print sum(s)
    return sum(s)

It works fine, but I really don't understand why if I flip the order of the return and print statements, it suddenly doesn't work anymore. Why is that?


The return statement tells Python to break out of the function. So any code that comes after a return statement won't be executed because Python already broke out of the function.