0

I am trying out print and return functions in different sequences. Why does the order in the code change the output?

This is my first post so I apologise if it is badly phrased.

My first code in the longer block shown in (3) contained:

def function_that_prints():
     print ("I printed")
    return ("I printed")
def function_that_returns():
    return ("I returned")

f1 = function_that_prints()
f2 = function_that_returns()

print (f1)
print (f2)

Result:

I printed
I printed
I returned

But it changes when you reverse them. See below.

def function_that_prints():
    return ("I printed")
    print ("I printed")

def function_that_returns():
    return ("I returned")

f1 = function_that_prints()
f2 = function_that_returns()

print (f1)
print (f2)

Result:

I printed
I returned

Expected result:

I printed
I printed
I returned

Why ?

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33

4 Answers4

4

When you reach a return in a function you will exit the function, everything after the return call will never be executed.

desmaxi
  • 293
  • 1
  • 13
  • Just to add to this/for completion (also something I've found useful) - where `return` sends a specified value back to the caller and exits the function, `yield` can send sequence of values back. – ron_g May 22 '19 at 12:39
1

You can see in the second example in the function function_that_prints that the line with the return comes before the line that prints. In Python and most other languages the function will end when it returns. So simply, the line after the return never is executed. If you want function_that_prints to print inside the function it must occur before it returns as in the first example.

Sorry about the lack of formatting. I can improve this answer when I get back to my laptop.

1

Example 1

def function_that_prints():
     print ("I printed") # Line 1
    return ("I printed") # Line 2

def function_that_returns():
    return ("I returned") # Line 3

f1 = function_that_prints() # Will print line 1 and store returned value in f1 and doesn't print line 2 string
f2 = function_that_returns() # Doesn't print, but stores return string in f2

# Prints both the returned values
print (f1) 
print (f2)

Example 2

def function_that_prints():
    return ("I printed") # Line 1
    print ("I printed") # Line 2

def function_that_returns():
    return ("I returned") # Line 3

f1 = function_that_prints() # Line 2 is never excecuted as function returns in line 1 and the returned string is stored in f1
f2 = function_that_returns() # Will just return the string from line 3 and store in f2

# Prints the returned strings
print (f1)
print (f2)
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
0

When you tell a function to return something , you are basically asking coming out the function unless you got if and else statement. Now when you say return inside a function after the return, your print is encapsulated inside function but its not part of the the instruction that would be executed.

Shyryu
  • 139
  • 1
  • 1
  • 9