I am having a code in which there is return 2 at some places and I am not able to understand its meaning. Anyone can help me to explain this return 2 meaning. Thanks in advance.
-
4How can we understand code that you do not show us? We cannot see your monitor from here. – NathanOliver May 31 '16 at 12:31
-
1[If it helps...](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – chris May 31 '16 at 12:34
-
1The answer depends on what the function that contains this code is supposed to do. Read the function's documentation -- that should explain what various return values mean. – Pete Becker May 31 '16 at 12:41
-
Seems like a Bad alloc in the case you show. – krOoze May 31 '16 at 12:43
2 Answers
The statement
return 2;
means the function, in which it is, returns value 2
.
The caller function may use that value as an indication of the callee function termination condition (in the excerpt given it possibly is a specific value with the hidden meaning assigned 'could not create new CATDocumentServices' or simply 'something went wrong'...).

- 9,381
- 2
- 21
- 35
In C++ the return
statement returns from the current function as in
void func(arg_type arg)
{
// some code
return; // can appear almost anywhere in the function body, including several times
// more code
}
Functions requiring a return value require such a value to be given in the return
statement as in
return_value_type func(arg_type arg)
{
// ...
return some_value_of_return_value_type;
// ...
}
In your case, it appears that the statements are taken from the body of a function returning an int
(or a related type convertible from int
). So, return 2
simply returns the value 2. Thus, if you have
int my_func()
{
// ...
CATDocument* pDoc = NULL;
auto hr = CATDocumentServices::New("CATDrawing", pDoc);
if (FAILED(hr)) return 2;
// ...
}
int val = my_func();
will assign 2 to val in case the path of execution taken went over the return 2
statement in my_func()
.

- 44,150
- 20
- 113
- 196
-
u mean if this function executes till return statement then value 2 will be saved in val finally...did I get this right as you said – deep sodhi May 31 '16 at 12:57
-