GenExpr - func w/ conditional returns fails to return input variable unless assigned to new variable
Hi,
I'm using GenExpr for the first time, and I have a strange issue.
I have a function with three input parameters that should return the value of input 1 or 2 depending on the value of input 3. Returning the parameter variables directly causes the conditional return to never happen, but it does work if I reassign the inputs to a new variable and return that instead.
Meaning I am able to get it to work, but I don't understand why this is, so I wanted to post here in case someone knows something or it's a bug. I haven't found anything similar mentioned in docs or other posts. FYI I'm working in Max For Live, but I've had no other issues that I know of. Thanks.
Demo:
I'm working only in Max For Live so a video was faster. To the right is the value of [out3] of [gen~] in a different window.
First I just show that "out1" is outputting to the number box.
I run the failing function, returning the input variables directly. It should return a or b (1 or 2) based on if c > 0, but b is not returned when the condition is true. No compilation errors, but it obviously runs since it's returning a.
I assign the inputs to a variable and return that instead, which makes it work.
You are overwriting the return value .)
it should be
if(c>0) return b;
else return a;
otherwise you are saying return b, but no, now return a regardless , and that is why when you change to test it works.
But I am sure you figured out already, since some days passed
Thanks for the reply!
I understand what you mean, but two returns can't occur during a single function execution, that would go against the purpose of 'return'.
A 'return' stops execution of it's function, returns to the point in code where the function was executed, and the program resumes from there. When a 'return' is hit, any remaining code in the function is not executed. This is why the 'else' can be dropped here; it's a common way of writing conditional returns.
You are right. Sorry, not a programmer, so I got confused.
But indeed, when deleting the return a, everything works. Maybe you can bug report https://cycling74.com/support/contact
No problem, sorry if I over-explained, I get carried away. I'm "officially" a programmer, and I'm confused most of the time, so don't worry. I wasn't sure if it was a bug when I posted this, but I'll report it now. Thanks again.
Opening the code tab in the toolbar reveals the code that is actually executed. It shows that the return statement inside the if-statement is omitted; apparently that is not allowed, although I wasn't able to find documentation about it.
I forgot about the code tab, thanks for reminding me. I'm still new to max.
Yes, the 'return' should definitely not be omitted. I'll report it as a bug now, so I'll mark this as resolved. Thanks for the help!