Confused with functions

So I am doing the practice for functions. It ask to
Declare and define a function brew_tea() that:

  • has no return value
  • has an std::string parameter of tea_type
  • prints "Brewing <tea_type> tea" to the terminal

My code is as follows

#include <iostream>

std::string brew_tea(std::string tea_type);

std::string brew_tea(std::string tea_type) {

  return "Brewing " + tea_type + " tea";

}

int main() {
  
  std::cout << brew_tea("masala");
  
}

This will print out
Brewing masala tea

yet it gives me this error
Did you declare a void function brew_tea() with an std::string parameter of tea_type ?

when i declare it as void it get errors which do not show any output
Where am i going wrong?

I think i figured it out. So I should be declaring void instead of std::string. Then defining void and not returning but having it printing. As follows

#include <iostream>

void brew_tea(std::string tea_type);

void brew_tea(std::string tea_type) {

  std::cout << "Brewing " + tea_type + " tea";

}

int main() {
  
  brew_tea("masala");
  
}

Did I explain this properly?

1 Like

Hello, @degsx20845821697, and welcome to the forums!

Yes. You summed it up quite nicely. The only thing wrong with your first attempt was that it wasn’t what the lesson was looking for. From your explanation, it appears that you understand the difference. Good job!

FYI: To paste code with its original formatting intact (how it looks now) please review How do I format code in my posts?

1 Like