Cpp void function return

return statement

Terminates the current function and returns the specified value (if any) to the caller.

Contents

[edit] Syntax

attr  (optional) return expression  (optional) ; (1)
attr  (optional) return braced-init-list ; (2) (since C++11)
attr  (optional) co_return expression  (optional) ; (3) (since C++20)
attr  (optional) co_return braced-init-list ; (4) (since C++20)
attr (since C++11) sequence of any number of attributes
expression expression, convertible to the function return type
braced-init-list brace-enclosed list of initializers and other braced-init-lists

[edit] Explanation

1) Evaluates the expression , terminates the current function and returns the result of the expression to the caller, after implicit conversion to the function return type. The expression is optional in functions whose return type is (possibly cv-qualified) void , and disallowed in constructors and in destructors.

3,4) In a coroutine, the keyword co_return must be used instead of return for the final suspension point (see coroutines for details).

There is a sequence point between the copy-initialization of the result of the function call and the destruction of all temporaries at the end of expression .

The copy-initialization of the result of the function call is sequenced-before the destruction of all temporaries at the end of expression , which, in turn, is sequenced-before the destruction of local variables of the block enclosing the return statement.

[edit] Notes

If control reaches the end of

  • a function with the return type (possibly cv-qualified) void ,
  • a constructor,
  • a destructor, or
  • a function-try-block for a function with the return type (possibly cv-qualified) void
Читайте также:  Css select no border radius

without encountering a return statement, return ; is executed.

If control reaches the end of the main function, return 0 ; is executed.

Flowing off the end of a value-returning function (except main ) without a return statement is undefined behavior.

In a function returning (possibly cv-qualified) void , the return statement with expression can be used, if the expression type is (possibly cv-qualified) void .

If the return type of a function is specified as a placeholder type, it will be deduced from the return value.

Returning by value may involve construction and copy/move of a temporary object, unless copy elision is used. Specifically, the conditions for copy/move are as follows:

Automatic move from local variables and parameters

The expression is move-eligible if it is a (possibly parenthesized) id-expression that names a variable of automatic storage duration whose type is

and that variable is declared

If the expression is move-eligible, overload resolution to select the constructor to use for initialization of the returned value or, for co_return , to select the overload of promise. return_value ( ) (since C++20) is performed twice:

  • first as if expression were an rvalue expression (thus it may select the move constructor), and
  • if the first overload resolution failed or
  • it succeeded, but did not select the move constructor (formally, the first parameter of the selected constructor was not an rvalue reference to the (possibly cv-qualified) type of expression )
  • then overload resolution is performed as usual, with expression considered as an lvalue (so it may select the copy constructor).

If the expression is move-eligible, it is treated as an xvalue (thus overload resolution may select the move constructor).

Guaranteed copy elision

If expression is a prvalue, the result object is initialized directly by that expression. This does not involve a copy or move constructor when the types match (see copy elision).

[edit] Keywords

[edit] Example

#include #include #include void fa(int i) { if (i == 2) return; std::cout  "fa(" i  ")\n"; } // implied return; int fb(int i) { if (i > 4) return 4; std::cout  "fb("  i  ")\n"; return 2; } std::pairstd::string, int> fc(const char* p, int x) { return {p, x}; } void fd() { return fa(10); // fa(10) is a void expression } int main() { fa(1); // prints its argument, then returns fa(2); // does nothing when i == 2, just returns int i = fb(5); // returns 4 i = fb(i); // prints its argument, returns 2 std::cout  "i = "  i  '\n'  "fc(~).second = "  fc("Hello", 7).second  '\n'; fd(); }
fa(1) fb(4) i = 2 fc(~).second = 7 fa(10)

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1541 C++98 expression could not be omitted if the return type is cv-qualified void it can be omitted
CWG 1579 C++11 return by converting move constructor was not allowed converting move
constructor lookup enabled
CWG 1885 C++98 sequencing of the destruction of automatic variables was not explicit sequencing rules added

Источник

Cpp void function return

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.

void sortbyName(vector& 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"; >

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.

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:

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,

void sortbyName(vector& 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"; >

If you need to return a value from your function then you can’t declare it to not return a value, a void return.

void sortbyName(vector& 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"; >

Then you have to declare your function to return a value, it can’t be declared with a return value of void.

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.

[ ]( 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 Person s.

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».

Источник

Оцените статью