return statement

What are those extra and seemingly pointless return statements in the void functions ??? What does it do? does it make any difference and is there any case where such statements are needed?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  {
  // Preconditions: none
  // Postconditions: strIn will be added to the messagePad
  void messagePad::add(const std::string& strIn)
  {
	messVect.push_back(strIn);
    return;
  }
   
  // Preconditions: indexIn must be a valid index of this object
  // Postconditions: if indexIn is valid (< messagePad.count()),
  //                   the message at indexIn will be removed.
  //                 if indexIn is invalid, messagePad will assert.
  void messagePad::remove(const size_t indexIn)
  {
    assert(indexIn < messVect.size());
    messVect.erase(messVect.begin() + indexIn);
    return;
  }
   
  // Preconditions: none
  // Postconditions: all messages will be removed from the messagePad
  void messagePad::clear()
  {
	messVect.clear();
    return;   
  }
Last edited on
They do nothing, and in these cases, are completely unnecessary.

The only time you would typically see a return in a void function is to exit early.

1
2
3
4
5
6
7
void foo()
{
  if (bar()) return;

  baz();
  quux();
}


BTW, you'll have an easier time doing your homework if you ignore others' solutions.
Thank you very much! it's not, by the way, my homework. I didn't do well in this class... so I am just going over the material again, trying to understand everything while I am on a break.
Ah, good man. Glad to have helped. (And sorry to have mis-thought.)
Last edited on
Topic archived. No new replies allowed.