• Post Reply Bookmark Topic Watch Topic
  • New Topic

return statement  RSS feed

 
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I have a question about return statement.
Code example:

When I write code as the code below:

Error: Missing return statement
Can anyone explain to me?
 
Greenhorn
Posts: 5
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you have written return statement within "if" condition in your second code which means that it will be executed only if "if" condition is satisfied. Otherwise there is no return. Therefore, there has to be some default value to be returned from the method.
 
Greenhorn
Posts: 1
C++ Redhat Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your code goes in to else section then there is not return section so you can write only  as below
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch all three of you.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhavalkumar Prajapati wrote:. . .
I don't believe that code will compile because line 5 doesn't return anything matching the return type of the method.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dhavalkumar Prajapati wrote:. . .
I don't believe that code will compile because line 5 doesn't return anything matching the return type of the method.


Yes, I think so.
If the method which contains the block code above need return type as primitives or object (except void method), after return keyword must be the same type with the type method need.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!