Search code examples
pythonfunctionreturn

Can we print inside a function with a return in Python?


I wonder if I can print a message inside a Python function that already returns a value. Would the print output appear when I call it from the main program, or will I just get the value returned?

For example:

def test(x,y)
    if x>y :
        print('x is bigger then y')
        return x
    else:
        print('y is bigger then x')
        return y

Solution

  • It depends on how you decide to call your function. Because your function has a return value, you would normally print out the results by doing print(test(x, y)) Doing this, however, would also print the print statement as well. If you were to just call the function test(x, y), you would get the print message, but it would not print the result for return x.