954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

creating your own function...?

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:

jmcparlin1
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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 :)

phorce
Posting Whiz
362 posts since Jul 2011
Reputation Points: 31
Solved Threads: 26
 

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 .....

metronomu
Newbie Poster
7 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

( 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:

MrEARTHSHAcKER
Junior Poster in Training
68 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
 

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 !!

adityatandon
Junior Poster
114 posts since Dec 2011
Reputation Points: 33
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: