Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Does Return Statement Exit from features as well as functions?

Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 65 PRO
Take the below example in a feature body:

        if (definition.inputMethod == InputType.DIAMETER_VALUE)
        {
            setVariable(context, definition.variableName, rounded);
            return;
        }

Should this not execute any further code within the feature? I am currently seeing that it doesn't, at least not without an else block. Which I guess makes sense, but I thought the return statement would override anything else and run no further lines.

Another interesting thing I noticed is that, it seems, things done above a throw aren't carried out if a throw is encountered. So in the below example, on the condition of something, change the value of a variable, and also provide an informative throw. But that seemed to negate the action of setting the variable. Is that expected behaviour? Or is throw not the right thing to call in this sort of case?

        if (!isundefinedoremptystring(something))
        {
            setVariable(context, definition.variableName, rounded);
            throw "message for the throw";
        }

Comments

  • Options
    caden_armstrongcaden_armstrong Member, User Group Leader Posts: 127 ✭✭✭
    Yes, a return statement in a feature main body will stop the feature from progressing. If you find that is not the case, perhaps your if statement is not being entered. Add a println statement in to double check.

    Throw is meant for exceptions. If you want all the code to run, and then stop and show a message, you can use reportFeatureWarning or reportFeatureMessage.
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    I have a ton more to learn about featurescript, but I've only ever used "return" in editing logic or a manipulator change function, in which case, It would be "return definition;", not just "return;"
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    caden_armstrongcaden_armstrong Member, User Group Leader Posts: 127 ✭✭✭
    @Evan_Reese I only use it when I am debugging. Throw a return statement into a feature half way to keep the rest from running.

    Return; isn't obvious in featurescript because you don't need to declare function return types. Void isn't a thing. 
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,066 PRO
    great to know! thanks
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
Sign In or Register to comment.