Whats wrong here and how to fix it... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
2nd Feb 2018, 3:26 PM
PRO
PRO - avatar
12 Answers
+ 3
Because without it, you wouldn't be able to access any variables from within the function after calling it outside of the function.
2nd Feb 2018, 5:23 PM
Faisal
Faisal - avatar
+ 4
def a(miles): km=1.6*miles return str(km)+"km" print ("ur answer is "+a(9))
2nd Feb 2018, 3:29 PM
visph
visph - avatar
+ 3
Well, when the return statement is called, then if immediately stops execution of the function and everything after it (within the function) will be ignored.
2nd Feb 2018, 5:27 PM
Faisal
Faisal - avatar
+ 2
The problem is that you aren't returning a value, meaning that after the function is run, no value would be able to print out. Instead of printing out the result in the function, change it so that it returns it: def a(miles): km=1.6*miles return str(km)+"km" print ("ur answer is "+str(a(9)))
2nd Feb 2018, 3:30 PM
Faisal
Faisal - avatar
+ 2
All print does is print a string to the console, but cannot return a value. Return allows for functions to have a value and to return variables after going through the statements. For example, say you have a function that returns a variable after multiplying it by 2, and the function is named "a". You would be able to assign a variable to this function, as the return keyword allows for that (ie. x = a(5)). This would not be possible if the print function were used.
2nd Feb 2018, 5:16 PM
Faisal
Faisal - avatar
+ 2
No problem! 😉
2nd Feb 2018, 5:30 PM
Faisal
Faisal - avatar
+ 1
yeah i know that return returns a value but in this case i dont want to assign any value to any variable...i just want to print the particular code ...
2nd Feb 2018, 5:19 PM
PRO
PRO - avatar
+ 1
so why should i use return in this case
2nd Feb 2018, 5:19 PM
PRO
PRO - avatar
+ 1
ohk...ohk...ohk...now i got everything what u explained me.....thank u faisal bro for explaing me...u seem to a really experienced programmer....
2nd Feb 2018, 5:28 PM
PRO
PRO - avatar
0
but whats the difference betn returning and printing
2nd Feb 2018, 5:11 PM
PRO
PRO - avatar
0
oh....i got what u r saying right now...but i think the function ends with return ....am i right or wrong
2nd Feb 2018, 5:24 PM
PRO
PRO - avatar
0
👍👍👍👌👌
2nd Feb 2018, 5:29 PM
PRO
PRO - avatar