Supertecnoboff
Supertecnoboff

Reputation: 6606

When to 'return' in an Objective-C method

Just a quick question. I was just wandering whether or not, I still have to "return;" even in a void function?

At the moment, even in methods which are not returning a variable/etc... I still have "return" at the end of the method.

So do I still need this? Because I swear without it, it does NOT return to where it was called from.

EG:

-(void)viewDidLoad {
    [super viewDidLoad];

    // Run other setup code....

    // Run method 1.
    [self method_01];

    // Run next bit of code....
}

-(void)method_01 {

     // Run code....

     return;
}

Do I still have to do it like the above example?

Thanks for your time, Dan.

Upvotes: 0

Views: 377

Answers (4)

Duncan Babbage
Duncan Babbage

Reputation: 20187

You do not need to call return in methods that are defined with void and thus do not return a value.

There are times when you would want to call return in such methods, such as if you want to exit out of the method without executing the remaining code, if a particular condition is met:

if (iHaveDeterminedIAmFinished) {
    return;
}

... // code that would otherwise execute.

Other than this, it would be bad practice to routinely include return at the end of every method. Every Objective-C method returns without exception, if it reaches the end of the method without a previous return. Therefore, this practice would not be more clear to a reader who has any familiarity with Objective-C. Indeed, it would likely confuse other developers reading your code who would be left wondering what the intention was. It would be likely to appear like something had been omitted from the end of the method, since there would be no reason for including this return otherwise. In short, I suggest it would be bad practice to include unnecessary return calls at the end of methods.

Because I swear without it, it does NOT return to where it was called from.

Something else is going on here. You may well need to figure out what it is, but it is not correct that the absence of return calls would prevent a return to the point of execution. Either it is returning, and you're not realising it for some reason, or something else is happening in your code.

Upvotes: 1

nprd
nprd

Reputation: 1942

If the return is at the end of the method, it doesn't make any difference.

-(void) doSomethingWithX:(int) X
{
     ..........................
     ........some Code.........
     ..........................

     return ;
 }

The control would reach the caller one the method execution completes. Marking a return does the same.

However in a condition like below,

-(void) doSomethingWithX:(int) X
{
     if(X>10)
     {
          return;
     }

     ..........................
     ........some Code.........
     ..........................
 }

The some code will not be executed if your X value is greater than 10. So, by default control return to the caller at the end of method. Use return if you want to force a return to caller in between the method execution.

Upvotes: 2

Choppin Broccoli
Choppin Broccoli

Reputation: 3076

You can do it either way. It should return automatically without an explicit return.

Upvotes: 0

Ian MacDonald
Ian MacDonald

Reputation: 14030

You do not. The method will return to its previous point of execution once it reaches the end of the current scope.

Upvotes: 2

Related Questions