so I'm kinda new to c++ (actually very new) and I was messing around with my code:
#include <iostream>
using namespace std;
string aString()
{
cout << "Car" << endl;
}
int main()
{
cout << "Word:" << aString() << endl;
return 0;
}
I tried to get something like: "Word: Car".
It ended up not working and showing a bunch of weird characters. My question is can a function return a string like an integer does?
Sorry if this is a stupid question.
My question is can a function return a string like an integer does?
Sure, you want to write
string aString()
{
return "Car";
// ^^^^^^
}
If the function declares a return type, you actually need to return something, otherwise you have undefined behavior.
The compiler should have issued a warning about that.
std::cout
is used to print out values at the terminal, not to return them from functions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.