#234 – Multiple return Statements

You can have more than one return statement in a method and they can appear anywhere in the method.  If the return type of the method is not void, you must have at least one return statement and all return statements must return a value of the appropriate type.

It’s normally considered good practice to have a single return statement at the end of the method, to make the code more readable.  But you could also do something like the following:

        public float GiveAgeInHumanYears()
        {
            if (Age < 1)
                return 0;
            else if (Age == 1)
                return 10.5f;
            else
                return 21 + ((Age - 2) * 4);
        }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: