-4

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.

deep sodhi
  • 25
  • 1
  • 8

2 Answers2

2

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'...).

CiaPan
  • 9,381
  • 2
  • 21
  • 35
0

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().

Walter
  • 44,150
  • 20
  • 113
  • 196