str::String return from function

Viewed 44

I want to return std::string str can somebody fill up ?

#include <iostream>
#include <string>

void display(std::string str) {
    std::cout << str << "\n";
    return  ?
}

int main() {
    std::string str = "Message";
    ? = display(str);
}

1 Answers

If what you want is to display the string, you can simply have :

#include <iostream>
#include <string>

void display(std::string str) {
    std::cout << str << "\n";
}

int main() {
    std::string str = "Message";
    display(str);
}

But if what you want is to get the string back from dispaly() ( which is useless as it'll be the same value as str ); then you can have something like this :

#include <iostream>
#include <string>

std::string display(std::string str) {
    std::cout<<str<< "\n";
    return  str;
}

int main() {
    std::string str = "Message";
    std::string stringFromDisplay=display(str);
}
Related