1

I'm a newbie who just starting out. I have been playing around with function and I can't understand why I get the output below from the code below:

Why doesn't it print the return value as well as text from a function in continuous lines, why does the output look like it is looping through the functions twice?

def add(a, b):
 print "ADDING %d + %d" % (a, b)
 return a + b
def subtract(a, b):
 print "SUBTRACTING %d - %d" % (a, b)
 return a - b
def multiply(a, b):
 print "MULTIPLYING %d * %d" % (a, b)
 return a * b
plus = add(1,1)
minus = subtract(1,1)
times = multiply(1,1)
print plus
print minus
print times

The output I get is:

ADDING 1 + 1
SUBTRACTING 1 - 1
MULTIPLYING 1 * 1
2
0
1
TimoSolo
7,3756 gold badges36 silver badges51 bronze badges
asked Apr 19, 2018 at 8:21
1
  • 1
    That's the execution order of your program. First you call all the functions, then you print the results. Commented Apr 19, 2018 at 8:25

1 Answer 1

4

Your code is written this way. First you execute all three functions. Then you print the results of all three functions.

plus = add(1,1) # ADDING 1 + 1
minus = subtract(1,1) # SUBTRACTING 1 - 1
times = multiply(1,1) # MULTIPLYING 1 * 1
print plus # 2
print minus # 0
print times # 1

If you want the results interleaved with the calculations, then interleave them.

plus = add(1,1) # ADDING 1 + 1
print plus # 2
minus = subtract(1,1) # SUBTRACTING 1 - 1
print minus # 0
times = multiply(1,1) # MULTIPLYING 1 * 1
print times # 1
answered Apr 19, 2018 at 8:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.