RETURN function misunderstanding


#1




Help anyonneee im stuck on this... in some earlier exercises i had a problem with the return command, ie doing what the tutorial said me to do while it didnt even explain its behaviour under multiple applications or what i shouldnt do... which was the case for all the functions excet that one... and now ive arrived to this special exercise 6. Practice Makes Perfect where i can indeed create the program but i dont understand the use of the return command... thanx for taking the time to consider my problem...


Replace this line with your code.


#2

Return really means return, it gives you something in return (back), when you call the function. For example lets say i have this function:

def multiply(a,b):
    return a * b

this function takes two arguments, and returns the result when called:

result = multiply(2,4)

now i stored the returned result in a variable called result. Now i can print the result:

print result

i can also call print directly on the function call:

print multiply(2,4)

Does this help?


#3

give me a few minutes to process that. am still not getting it.. i am trying to understand..


#4

Let me explain a differently, the moment i give you a piece of paper which contains the following equation:

2 + 2

then i give you another piece of paper on which you have to write the answer. On this paper paper you write 4, and hand me this over. Now, you behaved like a function with a return statement. You got something from me, did the math (function are used for more then math, but explaining functions with math works wonders) wrote the answer, and gave me the piece of paper with the result (4) in return :smiley:

This is what returns do. You hand them something, and they hand you something back


#5

aww man thanx so much for quick response... ^^
but nope :frowning: i still dnt get it...

def multiply(a,b):
return a * b
result = multiply(4,2)
print result
this works perfectly BUT

i did this and it ended up that it i rplace the return function with smething useless like l=0
def multiply(a,b):
l = 0
result = multiply(4,2)
print result
(to avoid an indentation error) the function dont work.. which means the return function is really important... from my understanding i undertand that the return function is going to help 'activate' the function but i dont understand what it does to the function... is there any part of the tutorial where the return function is explained...


#6

a function call also just print something:

def multiply():
    l = 0
    print l

Yea, learn python the hard way - return (link)


#7

ahhhh ookkaayyyyy i get it( i think so, still a bit confused ^^" )
the return function tells the function what it should do with the thing found in between the parentheses
is that so??


#8

I should have stuck to my example:

def multiply(a,b):
    return a * b
result = multiply(4,2)
print result

def multiply(a,b):
    print a * b
multiply(4,2)

the one return the results, which means you can store it and do something with it, the other just prints it. End of the line (program in this case)


#9

yea but it would also be stored if:
def multiply(a,b):
print a * b
result = multiply(4,2)
wats the point of using a return function then?


#10

guess i will just google it XD


#11

def multiply(a,b):
    print a * b
result = multiply(4,2)
print result

now result is empty, the function itself prints.


#12

aaahhhh yeeaaaa nowww i understanddd htanxx soo muchh maaannnnn :smiley: that was really helpful.... the return function stores the result and we can decide wether or not to print it, say

def multiply(a,b):
return a * b

just call it

multiply(4,2) here if the function does not print and instead return the console shows none because the result is just STORED

or print it

print multiply(4,2) here the function stores the value and prints it... return is used if i want to do smething else with the output.. which i would not be able to do with a print value...

thanx i totally understood :smile: )