Error: expecting return, found assignment statement

Hi guys,

return d1, d2; is giving me an error - expecting return, found assignment statement

this is a return statement at the end of a user defined function.

Any idea as to why I am getting this error?

@nhuurre any idea with this error?

@bgoodri is it possible to return two values from a user defined stan function?

The effect of rejection depends on the program block in which the rejection is executed. In transformed data, rejections cause the program to fail to load. In transformed parameters or in the model block, rejections cause the current state to be rejected in the Metropolis sense.28

In generated quantities, rejections cause execution to halt because there is no way to recover and generate the remaining parameters, so extra care should be taken in calling functions in the generated quantities block.

as per the above quote taken from user manual, the user defined functions can be used in the transformed data, transformed parameters, model and the function block, right?

the statement return d1, d2; is not valid because d1, d2 is not a valid expression.

agreed that the parser error message is misleading - having failed to recognize your return statement as a return statement, the parser tried to match a different grammar rule. confused parser, therefore confusing message - in this case, too specific.

1 Like

Not unless they are part of a vector, row_vector, or array.

The next Stan release will have better parser error messages:

--- Translating Stan model to C++ code ---
bin/stanc  --o=foo.hpp foo.stan

Syntax error in 'foo.stan', line 3, column 11 to column 14, parsing error:
     1:  functions {
     2:    real my_log1p_exp(real x) {
     3:      return 1.2, 3.3;
     4:    }
     5:  }

Ill formed expression followed by ";". Expect expression after "return".