Reputation:
Im completely new to C++ and Im trying to make my own "cmd". Still getting error: "|11|error: return-statement with a value, in function returning 'void' [-fpermissive]|"... Please help :(
Code here:
#include <iostream>
#include <string>
using namespace std;
void cmd(int command)
{
if (command == login)
{
cout << "Prikaz login neni k dispozici!";
return 0;
}
else
{
cout << "Prikaz ";
cout << command;
cout << " neni znamy!";
return 0;
}
}
int main()
{
cout << "Zadejte prikaz: ";
cin << command;
cmd(command);
return 0;
}
Upvotes: 0
Views: 79
Reputation: 4339
To expand on Philipp Braun's answer, the exact issue was that if a function is declared void
, it can't have an actual return value. [Specifically, it can only have a return value of type void
, which basically means it can return either nothing or the result of another function that itself returns nothing.]
// Valid. Function is void, and returns nothing.
void foo(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
return;
}
// Valid. Returns (with no return value) upon reaching end of function.
void bar(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
}
// Valid. Returns the result of foo(), which itself returns nothing.
void bat(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
return foo(arg, u, ments);
}
// Invalid. 3 is int, not void.
void baz(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
return 3;
}
// Valid again. Function is int, and returns int.
int quz(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
return 3;
}
// And formally invalid again. Returns (with no return value) at end of function, instead of returning an int.
// Causes undefined behaviour, which can be... well, anything, really.
int qaz(int arg, int u, int ments) {
doSomethingWith(arg, u, ments);
}
In a function declaration and/or definition, the first type, shown before the name, is the function's return type, and indicates what the function returns. If it's void
, the function isn't allowed to have a return value; any return
statements must either be used without an expression, or with an expression of type void
.
The return
statement takes the following form:
return /* expression */;
In this case, expression can be any valid expression (such as in return 1 + 1;
or return functionName();
), or can be omitted for functions with a return type of void
. Note that it doesn't have to be omitted for functions with a return type of void
; the following is a weird, but perfectly valid program:
#include <iostream>
void iReturnVoid() {
std::cout << "I return void." << std::endl;
}
void andSoDoI() {
std::cout << "I must inform you that ";
return iReturnVoid();
}
int main() {
andSoDoI();
}
In short, the type of expression has to match the type specified at the beginning of the function.
If a return
statement isn't explicitly provided, then the function returns upon executing its final statement; this is treated as if the function ends with return 0;
for main()
, or as if the function ends with return;
for any other function.
Also note that if any value-returning function returns without returning a value, whether explicitly by encountering and executing return;
or implicitly by reaching the end of the function, then it results in undefined behaviour. (main()
is immune to this happening implicitly, due to its implicit return statement returning 0
, but can have trouble with this if it encounters an explicit return
statement without a value.
I know this may be a tad complex for your first day programming, but it basically boils down to this:
return
statement should have the same type as the function itself. If the function is void
, it returns void
. If the function is int
, it returns an int
. If the function is char*
, it returns a pointer to a char
. And so on.main()
without encountering a return
statement, it implicitly inserts a return 0;
statement to indicate that the program completed successfully.return
statement, it implicitly inserts an empty return
statement, specifically "return;
". This is fine for void
functions, but can cause problems for functions with any other return type.Upvotes: 0
Reputation: 1573
No idea what you are exactly planning to do, but the following program should work. You simply did not declare your integer variables properly.
#include <iostream>
#include <string>
using namespace std;
int cmd(string command)
{
if (command == "login")
{
cout << "Prikaz login neni k dispozici!";
return 0;
}
else
{
cout << "Prikaz ";
cout << command;
cout << " neni znamy!";
return 0;
}
}
int main()
{
string command; /* Define String */
cout << "Zadejte prikaz: ";
cin >> command; /* Input */
cmd(command);
return 0;
}
Upvotes: 1