Ask coding questions

← Back to all posts
Im having problems with getting 2 of my functions too loop to each other.
MatthewMartin14 (0)

so i made 2 functions and at the end i want them each to go back to the other one but i keep getting this error: error: use of undeclared identifier 'b' i understand what the problem is but i haven't found a solution to it can somebody please help me figure it out?

Answered by xxpertHacker (864) [earned 5 cycles]
View Answer
xxpertHacker (864)

What you're looking for are forward declarations.

Normally, if an undefined/undeclared indentifier is parsed, the C++ parser stops immediately with a fatal error.

(identifiers are variable names, parameter names, function names, or type names)

Forward declarations declare it early, allowing code to be parsed correctly, and allowing early type checking.

void a(), b();
// ofc, you can separate them into:
// void a(); void b();

void a() {
    std::cout << "test";


void b() {
    std::cout << "test";


The early defined type is known as a "function prototype," whereas the actual function with it's body is known as the "function declaration."

Hopefully it helps.

MatthewMartin14 (0)

@xxpertHacker yes this did help me thank you so much!

BD103 (113)

When C++ builds, you are trying to reference a function that doesn't exist yet. Because C++ is compiled instead of interpreted, it scans through the function before running it. Because of this, you cannot make a recursive function loop in a compiled language. Try doing this in Python.

After realizing that my explanation was difficult to understand

Ok. Let's try this again. Imagine you were the C++ compiler, you would read the .cpp file line-by-line. Look at this code

#include <iostream> // Use iostream

void a() { // Starts defining a function
	std:cout << "test"; // Print "test"
	b(); // Calls unknown function! AHH!
// Raise error and stop

If this were Python code, it would be different.

def a(): # Starts defining function
	print("test") # I don't care, not being run
	b() # I don't care, not being run
def b(): # Starts defining function
	print("test") # IDC, not being run
	a() # IDC, not being run

a() # Starts function, sees that both are defined, and runs

CPP and Python handle defining functions diferently. If this were JS, it would create all the variables (functions are variables, technically) first, and then run the code.

I hope that explanation was a little more read-able :P

Highwayman (1455)

but of course, since C++ is compiled, all functions are in fact already declared. you just need to do a forward declaration in order to say "hey, this exists!"

int add(int, int); // hey, I exist!

void addloop(int x, int y) {

int add(int x, int y) { // this is how I work!
  int z = x + y;


xxpertHacker (864)


Because C++ is compiled instead of interpreted, it scans through the function before running it.

Totally irrelevant, it's just a language design choice, for example, Rust is compiled, but has function lookahead.

BD103 (113)

I guess that's a good point, but still they both parse functions differently. @xxpertHacker

BD103 (113)

That works, though honestly I don't know much CPP. @Highwayman

Highwayman (1455)

That's just how it is sometimes, but you got us there. :)

xxpertHacker (864)


...I don't know much CPP.

Phff, well, if it helps, I don't think you can know all of C++, I think I only finished learning everything up to C++17 once I learned this, but how would I know what I don't know anyway?