Using return in void function

Is it wrong to use return in a function?
If you take the example below. I'm using return in the if statements and it works fine for me(using xcode) but my teacher says that I can't do that, I don't understand why since it works fine for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortbyName(vector<Person>& persons)
{
    sort( persons.begin( ), persons.end( ), [ ]( const Person& lhs, const Person& rhs )
         {
             if(lhs.lastname != rhs.lastname)
             {
                 return strcasecmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0;  
             }
             else
             {
                 return strcasecmp(lhs.firstname.c_str(), rhs.firstname.c_str()) < 0; 
             }
             
         });
    cout << "\nList is now sorted by name \n\n";
}
Last edited on
closed account (E0p9LyTq)
Is it wrong to use return in a (void) function?

Using a return statement by itself in a void function is acceptable. You can NOT return any values.

1
2
3
4
5
void aFunc()
{
   // do stuff
   return;
}
I am a begginer, i have only been learning for about 5-6 months, but, from what i know, the void or int or anything else you put behind the name of the function sets the return type, meaning that this:
1
2
3
4
int foo(int bar){
  bar *= 3;
  return bar;
}

Is a function that can only return an int variable.
IF, it is void, it means that is is just a function, not a return function. Therefore,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortbyName(vector<Person>& persons)
{
    sort( persons.begin( ), persons.end( ), [ ]( const Person& lhs, const Person& rhs )
         {
             if(lhs.lastname != rhs.lastname)
             {
                 return strcasecmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0;  //strcasecmp är samma sak som stricmp i windows
             }
             else
             {
                 return strcasecmp(lhs.firstname.c_str(), rhs.firstname.c_str()) < 0; //strcasecmp är samma sak som stricmp i windows
             }
             
         });
    cout << "\nList is now sorted by name \n\n";
}

Should technically always return NULL, although, as i said in the beginning, I am practically a n00b
@FurryGuy I got errors in my code if I'm not using return before my values.
closed account (E0p9LyTq)
If you need to return a value from your function then you can't declare it to not return a value, a void return.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortbyName(vector<Person>& persons)
{
   sort( persons.begin( ), persons.end( ), [ ]( const Person& lhs, const Person& rhs )
   {
      if(lhs.lastname != rhs.lastname)
      {
         return;
      }
      else
      {
         return;
      }

   });
   cout << "\nList is now sorted by name \n\n";
}
@FurryGuy hmm okay, but then I lose my other content.
closed account (E0p9LyTq)
Then you have to declare your function to return a value, it can't be declared with a return value of void.

http://www.learncpp.com/cpp-tutorial/14-a-first-look-at-functions/
The return statement belongs as a part of the anonymous function (the lambda-expression) whose value is passed to sort().

The return type is deduced, and your code is valid.

Your teacher probably glanced at the code and missed the fact that the return belongs to the lambda expression. The lambda expression can be thought of as a nested function; returning from that doesn't return from the outer function, it returns from itself.

[A function returning void] should technically always return NULL

No, it doesn't return a value. That doesn't mean a NULL value, it means no value; void.
Last edited on
Okay thanks @mbozzi !
Just to clarify,
3
4
5
6
7
8
9
[ ]( const Person& lhs, const Person& rhs ) {
  if(lhs.lastname != rhs.lastname) {
    return strcasecmp(lhs.lastname.c_str(), rhs.lastname.c_str()) < 0;
  } else {
    return strcasecmp(lhs.firstname.c_str(), rhs.firstname.c_str()) < 0;
  }
}

Is a nameless callable object that accepts two constant references to Person and returns something else. This object is temporary since it has no name, but it gets passed to sort() when your program calls it. sort() calls the object you passed to it and uses the result to compare pairs of Persons.

It's important to realize that you aren't calling that anonymous function yourself, but rather giving it away to be used later. This is a very powerful idea; such functions that accept or return other functions are called "higher order".

With a slight modification these anonymous (or "lambda") functions become "closures", which are so powerful in a mathematical sense that they can be used as the foundation for building computations from nothing. FWIW, they're also the foundation of an entirely different approach to programming called "functional programming".
Topic archived. No new replies allowed.