+ 3
Whats wrong here and how to fix it...
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.
+ 4
def a(miles):
km=1.6*miles
return str(km)+"km"
print ("ur answer is "+a(9))
+ 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.
+ 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)))
+ 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.
+ 2
No problem! 😉
+ 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 ...
+ 1
so why should i use return in this case
+ 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....
0
but whats the difference betn returning and printing
0
oh....i got what u r saying right now...but i think the function ends with return ....am i right or wrong
0
👍👍👍👌👌