I tend to print the function
For starters you are going to output an object of the type std::string
and not a function. The function is used to set a value to an object of the type std::string
.
Let's consider your code step by step.
You are using class std::string
but have not included the corresponding header where the class is defined. It is implementation defined whether the header <iostream>
(that you are using)
#include <iostream>
internally includes the header <string>
.
So you need to include the both headers
#include <iostream>
#include <string>
Further, you have declared the function function
void function(std::string people);
and this function (not its returned object) is outputed in main
std::cout << function;
In this case the function designator is implicitly converted to pointer to the function and this pointer is used in the above statement as an right-side expression. Due to the operator overloading resolution the compiler selects the following overloaded function
basic_ostream& operator<<(bool n);
where the function pointer is implicitly converted to the type bool
and as the function pointer is not a null pointer because the function is defined then the above statement outputs the value 1
that corresponds to the boolean literal true
.
You have defined the function the following way
void function(std::string people){
people = "John";
}
It has one parameter that accepts an argument by value It means that the function deals with a copy of the value of the argument. And within its body the function changes the parameter that stores the copy of the argument. The function parameter is its local variable that will not be alive after exiting the function. The function does not uses the value of the passed argument because it at once changes the value of its parameter ignoring the passed argument. So the function as is does not make sense.
You can for example either pass to the function an object of the type std::string
declared in main by reference and change the object within the function or you can just return from the function an object of the type std::string
.
Here is a demonstration program.
#include <iostream>
#include <string>
void f1( std::string &people );
std::string f2();
int main()
{
std::string people;
f1( people );
std::cout << people << '\n';
std::cout <<f2() << '\n';
return 0;
}
void f1( std::string &people )
{
people = "John";
}
std::string f2()
{
return "John";
}
The program output is
John
John
The function f1
accepts the variable people
declared in main
by reference. So within the function the original (not its copy) variable people
is assigned a value (string literal "Join"
). The function f2
builds a new object of the type std::string
that at once is outputed in main
.
( ...arguments ... )
:std::cout << function(people);
()
to invoke the function (function(...)
). 2.function
should acceptpeople
by reference if you want the modification of it to be seen by the caller. These are fundamental things you can learn from a good book.