title: How to return a string from a C++ function?
body: <p>This is a simple sample program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  string replaceSubstring(string, string, string);
  
  int main()
  {
      string str1, str2, str3;
  
      cout &lt;&lt; "These are the strings: " &lt;&lt; endl;
      cout &lt;&lt; "str1: \"the dog jumped over the fence\"" &lt;&lt; endl;
      cout &lt;&lt; "str2: \"the\"" &lt;&lt; endl;
      cout &lt;&lt; "str3: \"that\"" &lt;&lt; endl &lt;&lt; endl;
      cout &lt;&lt; "This program will search str1 for str2 and replace it with str3\n\n";
  
      cout &lt;&lt; "The new str1: " &lt;&lt; replaceSubstring(str1, str2, str3);
  
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  string replaceSubstring(string s1, string s2, string s3)
  {
      int index = s1.find(s2, 0);
  
      s1.replace(index, s2.length(), s3);
  
      return s1;
  }
  </code></pre>
  
  <p>It compiles however the function returns nothing. If I change <code>return s1</code> to <code>return "asdf"</code> it will return <code>asdf</code>. How can I return a string with this function?</p>