0

I'm trying to figure out what's going on, and am really stuck:

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

def divide (a,b):
    print "DIVIDING %d / %d" % (a,b)
    return a/b


print "Let's do some math with just functions!"

age = add(30,5) # state A
height = subtract (78,4) # state A
weight = multiply (90, 2) # state A
iq = divide (100, 2) # state A

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq) # state B

I can't understand why in state A the outcome is the second line of the functions, where there's print, while in state B the outcome is the true output of the function, which I think is accomplished by return.

Apologize if the terminology is not very accurate.

Thanks

Amateur
  • 139
  • 1
  • 1
  • 6
  • 1
    I do not understand your question. Can you rephrase it? In the last line ("State B") there is no function call to add, subtract, multiply or divide. – user2737086 Feb 02 '14 at 20:46
  • Sorry if it's not clear; maybe since I've not get it right I'm not able to explain it any better. On the last line (state B) there's no direct function call, of course, but the assigned variables (age, height etc) are already connected to the functions where it's "state A". The question is: why when the first time the function are called, the first line of the function is printed, but the true output is not shown, while the second time, where there's an indirect function call (right term?) the true output is printed, while the first lines of functions are not printed anymore? – Amateur Feb 02 '14 at 21:37

2 Answers2

1

Go step by step.

In this line:

age = add(30,5) # state A

Because of the () and a name before a function is called. The arguments 30 and 5 are being put into actual values of parameters a and b and the execution goes into this code:

def add(a,b):
    print "ADDING %d + %d" % (a,b)
    return a+b

The first line prints "ADDING %d + %d" % (a,b) with a being 30 and b being 5. Then the expression a+b is being evaluated and returned. This causes the line:

age = add(30,5) # state A

to "change" (just conceptually - you can think about it this one, it really does not change of course) to:

age = 35 # state A

which just assigns the value 35 into the variable age. Next # state A lines go in the same way.

In the last line:

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

the results from function call are already remembered in variables age, height etc. At this point you may as well forget everything about the functions add, subtract etc. So here the interpreter will only call the internal implementation of the % operator that will put the values of (age, height, weight, iq) at their appropriate %d positions in the string, and it will print the result to the console.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • @user3263734 Because it is not printed. Only `a` and `b` are printed. Just read the code that's right in front of you ;) If you'll add `print a+b` statement in the `add` function for example you will also see the result in the first case. – BartoszKP Feb 02 '14 at 21:45
0

I'm in the process of learning python as well. It's worth noting that return is what the function evaluates to given a set of parameters and can be reused again (ie set to a variable), while print is simply meant to display an output in the console

Zach Russell
  • 1,060
  • 2
  • 13
  • 27