Priority & order of print statements involving function & not printing with return (follow up to a old forum post)


It's my first time posting to Codecademy, so pardon me for any faux pas being new to coding & this community.

Came across old forum response on Codecademy differentiating print and return using the following script:

def function_that_prints():
print "I printed"

def function_that_returns():
return "I returned"

f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2

Now I am wondering why "I printed" printed before anything else, since function wouldn't execute by merely equating it with f1?

No outright error, but
1. The order of print within a function seems to get called without actually calling.
2. A print statement didn't actually print when placed after a return inside a function (please see test_returnB in my messy amateur script)

1. The function shouldn't execute by merely associating it with f1?
2. actually print test_returnA AND test_returnB, rather than missing that test_returnB following a return

#Added test prints

print ("test_1")

def function_that_returns():
    print ("test_returnA")
    return "I returned"
    print ("test_returnB")


def function_that_prints():
    print ("test_printA")
    print ("I printed")
    print ("test_printB")

f1 = function_that_prints()
f2 = function_that_returns()

print ("Let's see what the values of f1 and f2 are")

print (f1)
print (f2)


this line:

f1 = function_that_prints()

will execute the function, which means print "I printed" will happen, and i printed gets printed. If anything inside the function is returned, it is now stored in a variable called f1. Using a single equal sign assigns a value to a variable, so i don't understand what you mean by: by merely equating it with f1. There is not equating, that is done with two equal signs. Either way, a function call is made, the function gets executed, and i printed will be actually printed

the function does executed, anything returned is stored in f1, that is what you are doing with that line of code. it is a function call (aka function execution)

Do realize, that a function ends, when a return keyword is reached.


Thanks for your explanation stetim94, helped me clear things up, and led me to read a lot more.

A follow up question then comes up as to whether it's more efficient in more complex codes to let functions execute immediately and store in the memory when associated with a variable like it did in this case when assigned to f1, or wait until the variable itself is actually referenced later in the code (if such thing is even do-able).

I really appreciate it for the quick and thorough explanation stetim94, look forward to doing more programming!


i think executing the function immediately rarely happens. It is better to have the function first, then the main "body" of your program, and call a function whenever it you need it (you might need separate function multiply times, and at different points in your program)