+ 6

Can anyone explain to me how returning something in a function exactly works?

I have recently learned functions and I am stuck on the return part. I have some idea of how it works. But not enough. Can anyone help me?

2nd Jul 2019, 8:01 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
14 Respostas
+ 8
Here is now a suggestion how the structure of main program and function could be: (i hope you don’t need to read it on a smartphone 🙂) you are very welcome to bring in your own ideas! A calculator in python by Default FUNCTION to calculate basic math operations arguments = num1, operator (+-/*), num2 look for operator and perform related operation return calculated result back to calling program MAIN PROGRAM do this until user inputs exit (loop) ask for parameters for calculation or exit to terminate program make input variable so that number of arguments can vary split parameters to a data structure check if input contains “exit” if yes leaf loop here check if input contains 2 numbers and a valid operator call function with arguments num1, operator, num2 receive calculated result back from function print result go for next loop iteration
2nd Jul 2019, 6:48 PM
Lothar
Lothar - avatar
+ 9
You can return any object or data type from a function. this can be string, integer, float, lists and some others more. Even if you do not return anything explicitly python will return something (None) back to the calling code. You can find here some information: https://www.sololearn.com/learn/Python/2287/
2nd Jul 2019, 9:43 AM
Lothar
Lothar - avatar
+ 8
ok Default - no problem. I have had a closer look on your code. There are some minor issues you could solve. But an other thing is an issue that you finally in your function have a retun statement. Return does not only send back result but it also terminates the function and give back the control to the main program. So before you start fixing some ussues i think it’s worth to think about structure and flow of the program. Don’t worry, you may use most of your code but may be at an other location in your code. I try to give you a kind of rough pseudo code that describes main program and function as well as thrir relstionship and interfaces. Give me a little time to do this.
2nd Jul 2019, 5:42 PM
Lothar
Lothar - avatar
+ 8
This pseudo code does not have any syntax but gives a description in a nearly natural language what a program should do.
3rd Jul 2019, 11:17 AM
Lothar
Lothar - avatar
+ 8
The idea is that you run the while loop in the main program and not in the function. Main programm takes input values and operator and pass this as arguments to the function. function then has to prepare submitted data, does the calculation and the use return to give result of calculation back to main program. I will try to do a basic version of this in the next days.
3rd Jul 2019, 12:22 PM
Lothar
Lothar - avatar
+ 5
Here‘s an example of how to use the return statement: ———————————— def add(num1, num2): return num1+num2 print(add(5,3)) >>>8 ———————————— You can find more info about return here: https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement
2nd Jul 2019, 8:06 AM
aceisace
aceisace - avatar
+ 3
Alright I looked at your answers and began working on a code to practice it on. It has some problem and I could really use your help figuring this out. https://code.sololearn.com/c1W7K9X2uneU/?ref=app
2nd Jul 2019, 12:45 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 3
Thanks for the effort. But I don't really know pseudo code. If possible can you make it more simple for me to understand? (I know pseudo is already simple but I don't know pseudo)
3rd Jul 2019, 10:43 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 3
Alright. I will use this as much as I can but how do we use return in this?
3rd Jul 2019, 11:52 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 3
Can you give me an example using a code? I think that will be easier.
3rd Jul 2019, 12:49 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 3
Btw I would just like to point out that I am really humbled by all the support . Really it means a lot to me.
3rd Jul 2019, 6:02 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 3
I went ahead and Made some more def experiments for practice. I have already made these but I thought I should practice more. If you have any suggestions that I could use to make them better or to better my understanding of "return". Just let me know https://code.sololearn.com/cvP1dda1w36J/?ref=app
3rd Jul 2019, 6:05 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 2
Lothar is returning nothing of some use?
2nd Jul 2019, 11:46 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar