What is the purpose of the return statement?

后端 未结 13 2413
难免孤独
难免孤独 2020-11-21 06:28

What is the simple basic explanation of what the return statement is, how to use it in Python?

And what is the difference between it and the print state

相关标签:
13条回答
  • 2020-11-21 06:55

    In python, we start defining a function with "def" and generally, but not necessarily, end the function with "return".

    A function of variable x is denoted as f(x). What this function does? Suppose, this function adds 2 to x. So, f(x)=x+2

    Now, the code of this function will be:

    def A_function (x):
        return x + 2
    

    After defining the function, you can use that for any variable and get result. Such as:

    print A_function (2)
    >>> 4
    

    We could just write the code slightly differently, such as:

    def A_function (x):
        y = x + 2
        return y
    print A_function (2)
    

    That would also give "4".

    Now, we can even use this code:

    def A_function (x):
        x = x + 2
        return x
    print A_function (2)
    

    That would also give 4. See, that the "x" beside return actually means (x+2), not x of "A_function(x)".

    I guess from this simple example, you would understand the meaning of return command.

    0 讨论(0)
  • 2020-11-21 06:56

    return means, "output this value from this function".

    print means, "send this value to (generally) stdout"

    In the Python REPL, a function return will be output to the screen by default (this isn't quite the same as print).

    This is an example of print:

    >>> n = "foo\nbar" #just assigning a variable. No output
    >>> n #the value is output, but it is in a "raw form"
    'foo\nbar'
    >>> print n #the \n is now a newline
    foo
    bar
    >>>
    

    This is an example of return:

    >>> def getN():
    ...    return "foo\nbar"
    ...
    >>> getN() #When this isn't assigned to something, it is just output
    'foo\nbar'
    >>> n = getN() # assigning a variable to the return value. No output
    >>> n #the value is output, but it is in a "raw form"
    'foo\nbar'
    >>> print n #the \n is now a newline
    foo
    bar
    >>>
    
    0 讨论(0)
  • 2020-11-21 06:57

    I think a really simple answer might be useful here:

    return makes the value (a variable, often) available for use by the caller (for example, to be stored by a function that the function using return is within). Without return, your value or variable wouldn't be available for the caller to store/re-use.

    print prints to the screen, but does not make the value or variable available for use by the caller.

    (Fully admitting that the more thorough answers are more accurate.)

    0 讨论(0)
  • 2020-11-21 06:57

    Best thing about return function is you can return a value from function but you can do same with print so whats the difference ? Basically return not about just returning it gives output in object form so that we can save that return value from function to any variable but we can't do with print because its same like stdout/cout in C Programming.

    Follow below code for better understanding

    CODE

    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)
    height = subtract(78, 4)
    weight = multiply(90, 2)
    iq = divide(100, 2)
    
    print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
    
    
    # A puzzle for the extra credit, type it in anyway.
    print "Here is a puzzle."
    
    what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
    
    print "That becomes: ", what, "Can you do it by hand?"
    

    We are now doing our own math functions for add, subtract, multiply, and divide. The important thing to notice is the last line where we say return a + b (in add). What this does is the following:

    1. Our function is called with two arguments: a and b.
    2. We print out what our function is doing, in this case "ADDING."
    3. Then we tell Python to do something kind of backward: we return the addition of a + b. You might say this as, "I add a and b then return them."
    4. Python adds the two numbers. Then when the function ends, any line that runs it will be able to assign this a + b result to a variable.
    0 讨论(0)
  • 2020-11-21 07:10

    Difference between "return" and "print" can also be found in the following example:

    RETURN:

    def bigger(a, b):
        if a > b:
            return a
        elif a <b:
            return b
        else:
            return a
    

    The above code will give correct results for all inputs.

    PRINT:

    def bigger(a, b):
        if a > b:
            print a
        elif a <b:
            print b
        else:
            print a
    

    NOTE: This will fail for many test cases.

    ERROR:

    ----  
    

    FAILURE: Test case input: 3, 8.

                Expected result: 8  
    

    FAILURE: Test case input: 4, 3.

                Expected result: 4  
    

    FAILURE: Test case input: 3, 3.

                Expected result: 3  
    

    You passed 0 out of 3 test cases

    0 讨论(0)
  • 2020-11-21 07:11

    Think of the print statement as causing a side-effect, it makes your function write some text out to the user, but it can't be used by another function.

    I'll attempt to explain this better with some examples, and a couple definitions from Wikipedia.

    Here is the definition of a function from Wikipedia

    A function, in mathematics, associates one quantity, the argument of the function, also known as the input, with another quantity, the value of the function, also known as the output..

    Think about that for a second. What does it mean when you say the function has a value?

    What it means is that you can actually substitute the value of a function with a normal value! (Assuming the two values are the same type of value)

    Why would you want that you ask?

    What about other functions that may accept the same type of value as an input?

    def square(n):
        return n * n
    
    def add_one(n):
        return n + 1
    
    print square(12)
    
    # square(12) is the same as writing 144
    
    print add_one(square(12))
    print add_one(144)
    #These both have the same output
    

    There is a fancy mathematical term for functions that only depend on their inputs to produce their outputs: Referential Transparency. Again, a definition from Wikipedia.

    Referential transparency and referential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program

    It might be a bit hard to grasp what this means if you're just new to programming, but I think you will get it after some experimentation. In general though, you can do things like print in a function, and you can also have a return statement at the end.

    Just remember that when you use return you are basically saying "A call to this function is the same as writing the value that gets returned"

    Python will actually insert a return value for you if you decline to put in your own, it's called "None", and it's a special type that simply means nothing, or null.

    0 讨论(0)
提交回复
热议问题