Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

(C++) return

 

Keyword that ends a function. Depending on the function, return might return a value.

 

 

 

const int Square(const int x)

{

  const int solution = x * x;

  return solution;

}

 

 

If a function has no return value (that is, a return type of void), one can omit the final return.

 

 

#include <iostream>

 

void SayHello()

{

  std::cout << "Hello" << std::endl;

  //return; //No need to

}

 

 

Exception: main

 

The function main is special. It returns an integer error code of the program, where a zero denotes a no-failure run. When main's closing bracket is reached, the effect is equivalent to (Standard, 3.6.1.5):

 

 

return 0;

 

 

Therefore, the following two pieces of code are equivalent:

 

 

int main() {}

 

 

 

int main() { return 0; }

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.