I'm new to C++, and I've been picking it up really well, but when it came to making my own function, I hit a problem. I've looked and I cant find the problem, its probably something really stupid but I cant find it. Heres the code, its really simple.
I'll put the code in bold.
From the top:

# include <iostream>
using namespace std;

int main()
{
   
    cout << "Hello!\n";
    
    testFunction ();

    cout << "Your back!\n";

    char f;
    cin >> f;
    return 0;
}


void testFunction ()
{
    cout << "Hello 2!\n";
}

I know its really simple but I dont see the problem with that. I read all through my C++ book and I found nothing that could help and I copied the code exactly. Also, the tutorials on youtube that I use on the side had exactly the same code.

The compiler I'm using is Dev-C++ 4.9.9.2. All I done was click the empty file icon and wrote the code, and I havent altered the settings on the comiler, its all default.
If anyone can help it would be very much appreciated. :confused:

Recommended Answers

All 5 Replies

Hey =)

It's kind of right, the only problem is that the function is declared after the main and when it's read in main, it doesn't know yet that the function exists. Two solutions:

Put the function before main:

# include <iostream>
using namespace std;

void testFunction ()
{
    cout << "Hello 2!\n";
}

int main()
{
   
    cout << "Hello!\n";
    
    testFunction ();

    cout << "Your back!\n";

    char f;
    cin >> f;
    return 0;
}

Or, create a prototype of the function:

# include <iostream>
using namespace std;

void testFunction();

int main()
{
   
    cout << "Hello!\n";
    
    testFunction ();

    cout << "Your back!\n";

    char f;
    cin >> f;
    return 0;
}


void testFunction()
{
    cout << "Hello 2!\n";
}

Hope that helped :)

You must DECLARE the function before using it. In this case you only DEFINED it after you used it. The compiler won't let you use it unless you at least DEFINE it before you use it. Search more and learn about defining and declaring functions and variables. In your case is simple , after :

using namespace std;

put

void testFunction();

....... rest of code .....

You must DECLARE the function before using it. In this case you only DEFINED it after you used it. The compiler won't let you use it unless you at least DEFINE it before you use it. Search more and learn about defining and declaring functions and variables. In your case is simple , after :

using namespace std;

put

void testFunction();

....... rest of code .....

you mean a function prototype like in phorce's post

( I don't want to repeat 3rd time what guys above had have already done, I just want to explain why you need to do that )

Compiler reads from the top to the bottom of code, just like you read the lesson from physics.

Now imagine that you read, and see some task with unknown formula, you won't do it because you have no idea where to search the formula definition.

But what if author has put formula before the task? You will do it ( that's like when you define entire function before main() ).

Or another example, author has noted where you can find formula like "See formula at page 13", when you reach task, you will find formula, and do the task.

Same with compiler, you declare function on the top of code, so compiler knows you will use it somewhere, so he will search for its definition in the code when he needs to call it.

:icon_wink:

Agree with everyone !!
That is your only problem... Rest of the code seems fine.. Try adding the function prototype or declaration ( as some ppl may call it )!! Its sure to work that way !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.