0
Please can someone explain the return statement in function like am 5?
Function
14 Answers
+ 3
Okundaye Jesse Eghosasere ,
we are going to help you, if you can give us a clear description of your task, not to forget a link to your code.
thanks!
+ 2
You can re-read the lessons as often as you like. Modify the example codes to see how it affects the output.
"return" means "give back". A function that returns a values, gives you the value it has calculated. Now you can use the returned value, e.g. store it to another variable or print it
+ 1
Okay so basically you want to know about return statement right?
Just assume one example:
you go to shop you give shopkeeper some money in return he gives you something which you want..
so same is with coding you call one function with some parameters and expects something to get returned which you want...
hence for any user-defined functions from where you want some kind of output you have to write return statement..
This is what basic return statement means.. if your query is yet not solve I'll request you to put it more clearly
Thanks,
+ 1
Think of a function as a part of an assembly line at a factory. Perhaps this machine on the assembly line takes a water bottle and puts a plastic label on it.
This machine (function) is going to have some sort of input and some sort of output.
The input for this machine would be both a water bottle and some sort of plastic label. The output would be a plastic bottle with a label on it. This output is what we put in the return statement.
If we were to think of programming the factory machine, we would write something like this:
function machine(bottle, label):
labelled_bottle = bottle + label
return labelled_bottle
That return statement tells us exactly what is popping out the other side of the machine AND it tells us that the machine is done doing its job.
0
Thank youLothar
0
Thank you Lisa
0
Thank youSlick
0
A function with a return statement returns a value which can be assigned to a variable(or manipulated in anyway you deem fit). A function without a return statement just does something (or nothing) depending on how you define the function.
0
Kachi Emmanuel
Can I use while or for loop with a return statement?
0
Kachi Emmanuel
Thank you
0
Okundaye Jesse Eghosasere
return is used to mean "give back" some result.
I think you are thinking it as return to mean "go to" somewhere, which is a wrong concept.
0
Okundaye Jesse Eghosasere
Study this instance:
#this function returns a value which can be assigned to a variable
def iAmNone():
return None
#this function doesn't return anything; it just prints a text for each iteration of the for loop
def printSomeNones():
for i in range(2):
print("some Nones")
var1 = iAmNone() #can do this since the function returns a value
print(var1)
printSomeNones() # can't assign this to a variable since it doesn't return anything
Result...
None
some Nones
some Nones
0
See it as a function that gives you feedback of your instructions in a program. Now see the instructions as your code, telling the program to do a particular task and asking for a feedback via your return function.