1

Firstly, I'm only a beginner, so please forgive me. I have one question asking " Define a function called avg. It asks the user for three numbers and prints their average." Then another question asking "Define a function called avg4. It asks the user for four numbers and returns the average of four numbers"

I would just like to know if this is a trick question? Because from my understanding, 'print' prints the output in the console, whereas 'return' is part of a function and doesn't actually have any output in the console.

Here is my code for the first question, what would be the difference for the second question? (aside from the four numbers instead of three)

def avg(a, b, c):
    sum = (a+b+c)/3
    return sum

a1 = avg(float(input("First number: ")), float(input("Second number: ")), float(input("Third number: ")))
timgeb
  • 76,762
  • 20
  • 123
  • 145
oneman
  • 811
  • 1
  • 13
  • 31
  • Its common to use the two words interchangeably, just to avoid using the same word, when writing assignments. From the context, that "return" would be the same as "print", given that there is no functional context, as you mention. Also, you should edit your question because you code is not written properly. – BVJ Mar 12 '16 at 22:03

3 Answers3

3

I have no idea whether its a trick, its a poorly worded question or the writer is accurately describing what is needed... but you should stick with the literal description and do what it asks. avg should print the average and return None and avg4 should return the average. If there is any controversy later ("don't do what I said, do what I wanted"), you can rightly claim you did what was asked.

In a high-stakes job environment you would go back and get clarification, so talking to whoever gave you the assignment is reasonable too.

def avg():
    """Ask the user for three numbers and print the average"""
    numbers = []
    for i in range(1, 4):
        numbers.append(float(input('Number {}: '.format(i))))
    print('Average', sum(numbers)/3)

def avg4():
    """Ask the user for 4 numbers and return the average"""
    numbers = []
    for i in range(1, 5):
        numbers.append(float(input('Number {}: '.format(i))))
    return sum(numbers)/4)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
2

Any function a returns a value - which you could then assign to a name y or pass to another function b, for example y = a(x) or z = b(a(x)).

In a function definition you can have any number of return statements: when the function is called, the statements are executed until it encounters any return something statement, then a reference to something is given back; and any following statements are ignored.

In fact, if you do not type a return statement explicitly, then None will be returned implicitly at the end of the function.

The print statement does not interfere with any of this. It just prints what you tell it, and then the code proceeds. The printed value does not have to be the value that is returned from the function. That's a big difference.

timgeb
  • 76,762
  • 20
  • 123
  • 145
Ilja
  • 2,024
  • 12
  • 28
  • Okay thank you, so if I'm being asked to `print` a statement, they're wanting some output on the screen, but if they ask me to `return` a statement, they just want me to define a function but not have any output on the screen? – oneman Mar 12 '16 at 22:08
  • Yes I had `print(a1)` at the end of it, but someone edited my question and took it off for some reason. – oneman Mar 12 '16 at 22:10
  • Your function can print without returning (well, as I said, it will implicitly return None) in the first case, or return without printing. – Ilja Mar 12 '16 at 22:10
  • @timgeb - you edited a lot which was not written well: but one point I wrote like this intentionally and you edited wrongly ;) ... : you do not get the value of `something` returned, but a pointer on it (yes, this can make a difference, if a mutable from a higher scope was passed to the function and then gets returned, changing the outcome will have impact on the original source). Am I wrong? – Ilja Mar 12 '16 at 22:19
  • @Ilja you are right, "a reference to something" would be more precise. – timgeb Mar 12 '16 at 22:26
  • ah, thank you, I was right :) - well, that's why I originally wrote just `something`, to make it sound less complicated. – Ilja Mar 12 '16 at 22:33
  • So if I submitted this, it would be right yes? `def avg(a, b, c): sum = (a+b+c)/3 print(sum) a1 = avg(float(input("First number:")), float(input("Second number: ")), float(input("Third number: ")))` – oneman Mar 12 '16 at 23:32
  • And `def avg4(a, b, c, d): sum = (a+b+c+d)/4 return sum a1 = avg4(float(input("First number: ")), float(input("Second number: ")), float(input("Third number: ")), float(input("Fourth number: "))) ` – oneman Mar 12 '16 at 23:32
  • sorry that its all messy but I don't know how to change that – oneman Mar 12 '16 at 23:33
  • Well, these are two functions of which the first prints and the second returns the average of the passed arguments. But to be exact, it is not your function, that prompts the user to input the numbers: you do outside the function and pass them to avg(). As opposed to that, in the solution of tdelaney the prompt is inside the function. – Ilja Mar 13 '16 at 12:15
0

The difference can be subtle, but if you take the words to their root meaning it'll be a fundamental difference.

Return is usually associated with a function (though not necessarily!). Functions may return a value, specially if you think of functions in the classical analogy of a machine that takes in something and spits out a product. So basically you'll say x returns something when x is a function or some analogous structure.

Print has a more crude meaning, it is, in general, simply to display something on the screen. You may print something without it being the return of a function, or you might print outside of a function altogether. The idea here is that printing can be seen as a specific type of return, but not as a synonym.

Read more about returning and printing

Bernardo Meurer
  • 2,295
  • 5
  • 31
  • 52