My question is about recursion, but is slightly more complicated than than the typical case. Usually, a recursive function calls itself:
int functionName(int x) {
if (x meets condition) {
return x;
}
else {
x = functionName(x)
}
}
However, I have two functions: functionA
and functionB
. I want functionA
to call functionB
and functionB
to call functionA
:
int functionA(int x) {
if (x meets condition) {
return x;
}
else {
x = functionB(x)
}
}
int functionB(int x) {
if (x meets condition) {
return x;
}
else {
x = functionA(x)
}
}
We have a kind of paradox here where functionA needs to be defined before functionB and functionB needs to be defined before functionA.
Presumably, if we have function prototypes appear before the function definitions, we should be okay:
int functionA(int x); // PROTOTYPE
int functionB(int x); // PROTOTYPE
// [insert definition of functionA here]
// [insert definition of functionB here]
However, these inextricably linked processes are rather complicated. If we put both functions inside the same file, we will get what I call the "wall of text effect". The file will be rather long and hard to read. I am tempted to put the two different functions into two different files. However, if I do this, I am not sure what to do about #include
statements and header files. If the file function2.c
has an #include function1.h
and function1.c
has an #include function2.h
it seems kind of circular.