I made a stupid error when programming a function. I used> return(OR^2+6*OR+1)/(OR*se^2)Being parenthesis blind it took me half an hour to find the reason for the nonsensical results I got. I should have written> return((OR^2+6*OR+1)/(OR*se^2))Having said that why is the first variant (which returns the value of the numerator only) not a syntax error? I would have expected R to report something like> Error: unexpected '/' in "return(OR^2+6*OR+1)/"If this is not an error what is its purpose?
`return` is a function. I am away from my computer right now so I can't check, but I would expect you could create your own function named that. Other languages have `return` statements instead. On Wed, Apr 30, 2025, 02:17 Ralf Goertz via R-help <r-help at r-project.org> wrote:> I made a stupid error when programming a function. I used > > > return(OR^2+6*OR+1)/(OR*se^2) > > Being parenthesis blind it took me half an hour to find the reason for > the nonsensical results I got. I should have written > > > return((OR^2+6*OR+1)/(OR*se^2)) > > Having said that why is the first variant (which returns the value of > the numerator only) not a syntax error? I would have expected R to > report something like > > > Error: unexpected '/' in "return(OR^2+6*OR+1)/" > > If this is not an error what is its purpose? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Wed, 30 Apr 2025 11:15:02 +0200 Ralf Goertz via R-help <r-help at r-project.org> wrote:> If this is not an error what is its purpose?From the point of view of the R syntax, everything is an expression. One of the uses of return() being an expression is base::callCC(). The ability to use it inside an expression makes it possible to put it into a delayed-evaluation object and offer it as a continuation point to another function. Admittedly obscure, but sometimes useful. -- Best regards, Ivan
The R antibugging practice here is to not use the return function. The expression on the last line of the function is automatically the return value of the function. Some people use return within conditionals in the function, but when I have conditional execution flow I prefer to simply assign alternate return values to a local variable along different execution paths and require all execution paths to reach the end of the function where that variable name is alone on the last line. On April 30, 2025 2:15:02 AM PDT, Ralf Goertz via R-help <r-help at r-project.org> wrote:>I made a stupid error when programming a function. I used > >> return(OR^2+6*OR+1)/(OR*se^2) > >Being parenthesis blind it took me half an hour to find the reason for >the nonsensical results I got. I should have written > >> return((OR^2+6*OR+1)/(OR*se^2)) > >Having said that why is the first variant (which returns the value of >the numerator only) not a syntax error? I would have expected R to >report something like > >> Error: unexpected '/' in "return(OR^2+6*OR+1)/" > >If this is not an error what is its purpose? > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide https://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.