|
It has been mentioned that the original procedure and procedure-1 are equivalent i.e. Original procedure:
Procedure #1:
Please clarify the following in this respect: Q: Can a Python procedure return two values? For example can it return both a and b? I am asking this because in the original procedure b is returned only if test(a) becomes true. On the other hand the statement "return a" is outside the if... condition. So it should execute anyway. Now if test(a) evaluates to false, only a is returned. But if test(a) evaluates to true then b is returned as well as a .... is my understanding correct or wrong? If so, how is Procedure #1 equivalent to the original procedure? It either returns x or y, depending on test(x), but not both. |
|
A return automatically ends execution of the procedure and returns the parameters. So in that code there, ONLY b is returned if test(a) is true. the program never actually reaches the return a code. Also, yes, you can return multiple values, for example; |
|
I think the first proc use the shortcut instead of else keyword?? I also think you can return two things like this: return a, b. I do not know for sure if you can return two things consecutively, like this: |
|
Ok...I think I get it. Once a return statement is issued, the execution of the procedure stops and it exits (that's the general behavior of return in most other languages...which I forgot :( ). So, if two separate return statements are there, only one should execute. As for returning two variables in the same return statement, maybe that would work. I'd check. Thanks for replying. |