4

I have this code. How do I do this without creating an error?

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}
Noah
  • 120
  • 1
  • 8

8 Answers8

8

This is a case for a forward declaration. A forward declaration tells the compiler that the name is going to exist, what it's type is and allows you to use it in a limited context before you define it.

int function2();  // this lets the compiler know that that function is going to exist

int function1() {
    if (somethingtrue) {
        function2(); // now the compiler know what this is
    }
}

int function2() { // this tells the compiler what it has to do now when it runs function2()
    //do stuff
    function1();
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
3

Use forward declaration. You can declare a function before using it by just writing its signature, followed by a semicolon. With that, you promise the compiler that there will be a function definition with the same signature somewhere else.

int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}
maddin45
  • 737
  • 7
  • 17
2

Just put this above your functions:

int function1();
int function2();

But don't create an endless loop! With that two lines you tell the compiler that function1 and function2 will be defined in the future. In bigger projects you would use header files. There you do the same but can use the functions in multiple files.

And also don't forget the return statement. I think your code example was only demonstration but I only want to mention it.

In C++ you must seperate declaration and definition. Read more about it here: https://stackoverflow.com/a/1410632/4175009

Community
  • 1
  • 1
Entwicklerpages
  • 126
  • 1
  • 7
  • There is no reason to forward declare `function1` in this case. – NathanOliver Feb 05 '16 at 16:54
  • @NathanOliver Yes, in this case. But if you have many functions there is a point it is a better style to declare them all at the beginng (or better, in a header file). So I put the function1 for good style and completeness. – Entwicklerpages Feb 05 '16 at 16:56
1

Declare second function before first, so it can see it's existence.

int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1(); //beware of double recursion.
}
xinaiz
  • 7,744
  • 6
  • 34
  • 78
1
int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

This is called forward declaration. You let the compiler know there is a function called function2(), without defining it. This is enough for the compiler to insert a call to that function when it's called inside function1(), which is then resolved at link-time.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – NathanOliver Feb 05 '16 at 16:46
  • 2
    The answer should basically be complete before you post it. Being the first to answer is not what you should strive for. – NathanOliver Feb 05 '16 at 16:48
  • @NathanOliver Faster answers usually are the ones that get accepted. – Rickest Rick Feb 05 '16 at 16:50
  • I'm following a rule that I've read here, about posting an answer first, then expanding it. Is it a false rule? – iksemyonov Feb 05 '16 at 16:50
  • 2
    @iksemyonov It is a rule for people more concerned with getting rep then quality. – NathanOliver Feb 05 '16 at 16:51
  • 1
    @user1487195 That really shouldn't be the goal. We want quality answer and a block of code is not a quality answer in most cases. – NathanOliver Feb 05 '16 at 16:52
  • I Agree. But that is why code gets pushed quickly, then edits fill out the explanation. – Rickest Rick Feb 05 '16 at 16:54
  • @user1487195 As you can see that doesn't always work so well. My answer was a minute or two after but since it was complete(baring some typos I will admit) it became the most upvoted answer. – NathanOliver Feb 05 '16 at 16:59
  • Yes, and I actually voted for your answer because I did like how complete it was, but I would not have cared if your answer was complete from substitution or if you had to edit it. – Rickest Rick Feb 05 '16 at 17:09
1

Forward declare function2().

int function2(); //forward declaration

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}
Arne Böckmann
  • 467
  • 2
  • 16
1

This will solve your problem:

int function2();

int function1() {
    if (true) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}
bemul12
  • 413
  • 2
  • 4
  • 13
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – NathanOliver Feb 05 '16 at 16:48
0

The usual way to do this is to first declare your functions. The general convention is to put declaration in *.h file, and definition in *.cpp file. And then include *.h file in *.cpp file. That way you can use function1 and function2 in other files as well without repeating declaration. You just inlcude functions.h in that other file.

// functions.h
int function1();
int function2();

// functions.cpp
#include "functions.h"

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

However if you have a lot of functions and you want quick and dirty workaround you can do it like this:

class Root{public:

    int function1() {
        if (somethingtrue) {
            function2(); /// That will work now
        }
    }

    int function2() {
        //do stuff
        function1();
    }

    /// You can also put here variables, structs, classes, etc.
    /// and they will see each other regardless of order in code.

    /// But don't put main here
    /// it won't work

} root;

/// but now to call functoin1 or function2 from outside we need to use root
void main(){
    root.function1();
}

Root and root is just en example here. You can use any unused class name and variable name.

LovelyHanibal
  • 317
  • 2
  • 13