• Post Reply Bookmark Topic Watch Topic
  • New Topic

return statement in method  RSS feed

 
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does the return statement in the first if statement do?

if(copyCount == 1){




copyCount++;
return;
}
if(copyCount == ONE_IMAGE_CROPPED){

copyCount++;
}
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To explain take this example.
public class TestReturn{
public static void main(String args[]){

if(true){

System.out.println("Should print this");

return;
}

System.out.println("Should NOT print this");
}
}

In the above example if you see, on some condition you don't want to continue executing the complete method but just come out of this. Break would only break out of condition or loop but return will help you to break out of method. In the above cause your method return void hence explecite mention of void is not required.....
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nikos Katsikanis:
what does the return statement in the first if statement do?

if(copyCount == 1){




copyCount++;
return;
}
if(copyCount == ONE_IMAGE_CROPPED){

copyCount++;
}


The method execution will at return statement. Next lines of the method after return would not be executed.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return would stop execution and return the control to the caller. Since the called function doesn't return anything in this case the return type of such function has to be void in order to compile the code.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks for your advice
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!