Skip to main content
  1. About
  2. For Teams
Asked
Modified 5 months ago
Viewed 111 times
-5

I'm still new at functions and I tend to print the function which I don't know how to print the declared function. It prints "1" and not "John".

#include <iostream>
void function(std::string people);
int main(){
    std::cout << function;
    return 0;
}
void function(std::string people){
    people = "John";
}
5
  • Funcations need to be called (using ( ...arguments ... ): std::cout << function(people);
    Ted Lyngmo
    –  Ted Lyngmo
    2025-04-18 11:58:48 +00:00
    Commented Apr 18 at 11:58
  • 3
    1. You need () to invoke the function (function(...)). 2. function should accept people 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.
    wohlstad
    –  wohlstad
    2025-04-18 11:59:30 +00:00
    Commented Apr 18 at 11:59
  • I'll learn more thank you very much.
    NAYbabal
    –  NAYbabal
    2025-04-18 12:08:44 +00:00
    Commented Apr 18 at 12:08
  • That's the spirit. I suggest consulting this
    catnip
    –  catnip
    2025-04-18 19:51:42 +00:00
    Commented Apr 18 at 19:51
  • this question has been asked before
    vatsal
    –  vatsal
    2025-05-07 12:10:20 +00:00
    Commented May 7 at 12:10

3 Answers 3

2

Make sure that:

  • function returns std::string both at the declaration and at the definition
  • you remove the people parameter from function both at the declaration and the definition
  • you return "John" instead of setting people to it, so your function will have a value to be printed
  • you invoke function to execute it and get its return value
#include <iostream>
std::string function();
int main(){
    std::cout << function();
    return 0;
}
std::string function(){
    return "John";
}

Fiddle: https://www.mycompiler.io/view/79tu89qJbze

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

The issue is that in your code, you're trying to print the function pointer, not the result of the function call. In C++, when you write std::cout<< function; you are printing the memory address of the function itself, which is why it outputs 1.

To print "John", you need to call the function and then print the result.

The corrected version of your code:

#include <iostream>

// Declare the function that modifies the string
void function(std::string &people);

int main() {
    std::string name = "";  // Declare a string variable to hold the name
    function(name);  // Call the function to modify the string
    std::cout << name;  // Print the modified string
    return 0;
}

// Define the function that sets the value of the string to "John"
void function(std::string &people) {
    people = "John";
}

Check out one of these websites to discover the basics of functions in C++:

1 Comment

W3Schools is terrible. GeeksforGeeks is terrible. LearnCpp is okay. A good C++ book is also okay.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.