1

I am writing a program. And in the main function "int main()" i call to a function called, lets say, "int X()". Inside "int X()" I would like to call to another function "void Y()". Any ideas how to do this? I tried, inside the X() function, doing "Y();" and "void Y();" but to no prevail. Any tips on getting this to work? if at all possible?

ex.

#include<iostream>

int X()
{
   Y();
}

void Y()
{
   std::cout << "Hello";
}

int main()
{
   X();
   system("pause");
   return 0;
}
5
  • 1
    You need to define Y before X so X knows about it when it uses it. You also need a "using namespace std;" Commented Jan 4, 2014 at 19:59
  • 2
    You need to read your C++ book again. Commented Jan 4, 2014 at 20:09
  • 8
    No, no, no. No using namespace std;. You suck. Please stop giving advice to people. Commented Jan 4, 2014 at 20:13
  • did you try this? got any error? Commented Jan 4, 2014 at 20:25
  • Thanks! Turns out I just had to put the function Y() above X()! We are using books that came out roughly 15 years ago so it tends to leave out some key info :/ Commented Jan 4, 2014 at 21:38

5 Answers 5

6

You must declare Y() before using it:

void Y();

int X()
{Y();}
Sign up to request clarification or add additional context in comments.

Comments

3

You must define or declare your functions before you use them. For example:

void Y();         //this is just a declaration, you need to implement this later in the code.
int X(){
    //...
    Y();
    //...
    return someIntValue;   //you will get warned if function supposed to return something does not do it.
}

OR

void Y(){
    //code that Y is supposed to do...
}

int X(){
    //...
    Y();
    //...
}

When you call the function you do not write its type anymore (to call functon Y you write: Y(arguments); and not void Y(arguments);). You write the type only when declaring or defining the function.

3 Comments

Awesome! Good to know, we used to write the "void" or "int" before we called the function alot.
That probably caused quite some problems (compiler may refuse to compile such code, as it would consider using name of the funcion preceded by its type many times as multiple definitions of the same function.) If you found this answer helpful you may mark it as accepted.
I would love to! But this is my first post and I only have a Rep of 1 so im not allowed :/
2

When the compiler reaches:

int X()
{
   Y();
}

it doesn't know what Y is. You need to declare Y before X by inverting their declarations:

void Y()
{
   std::cout << "Hello";
}

int X()
{
   Y();
}

int main()
{
   X();
   system("pause");
   return 0;
}

You should also provide a return value for X, otherwise a warning will pop up.

And please, don't follow the suggestion of using using namespace std;. The way you writing std::cout is just fine.

And here is the working example.

Comments

1

You need to declare the Y function before the X function uses it.

Write this line before the definition of X:

void Y();

Comments

0

You need to have cout << X(); X() it's just for void function!

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Welcome to SO! Unfortunately, I am voting to delete your answer on two reasons: 1. It does not really solve the problem (you can do X() even if is void and the problem is that Y is defined only after X). 2. There already is an accepted answer that solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.