1

Today I was trying to do recursion with multiple functions and I was using some function and in that I was using a function which is declared below it

Here is my code :

#include<bits/stdc++.h>
using namespace std;
#define MOD 10

int f(int x){
    if(x == 4) return 1;
    return 3*f(((2*x+2)%11)-1);
}


int q(int x){
    if(x == 7) return 1;
    return f(x) + q((3*x)%MOD);
}

int g(int x){
    if(x == 0) return 1;
    return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}

int h(int x){
    if(x == 0) return 1;
    return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}


int main() {

    cout << g(4);

    return 0;
}

The error is that in the function g(x) , it is accessing h(x) which is declared below and h(x) function is using g(x) function so not able to do anything

Please let me know what should I do to make this work.

Thanks a lot.

t.niese
  • 39,256
  • 9
  • 74
  • 101
Sudheera Y S
  • 59
  • 1
  • 6
  • 2
    Do you know about declaration and definition of a function? Also do not include: https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. – RoQuOTriX Jan 23 '20 at 08:45
  • 3
    Read about the difference between declarations and definitions in your favourite C++ book. – molbdnilo Jan 23 '20 at 08:45
  • Your function `h(x)` is not only declared below `g(x)` - it is DEFINED. `g(x)` needs to be declared before the point it is called in `g(x)` (aka, somewhat informally, a "function prototype") but need not be defined. [A function definition is a type of function declaration, which essentially implements the function. Each function (or overload of a function) may be declared many times, but only defined once.] – Peter Jan 23 '20 at 08:49
  • 1
    Use function prototypes, [Link](https://codescracker.com/cpp/cpp-function-definition.htm). – Varun Mukundhan Jan 23 '20 at 08:47
  • [Why should I not #include ?](https://stackoverflow.com/Questions/31816095) – t.niese Jan 23 '20 at 10:12

3 Answers3

4

So you are seeing the problem that a function is defined after you used it, so your compiler can't see it. This "problem" is solved with a declaration of the function and then later to define it. In your case you could declare all functions above main and then define(implement it) after main:

#include <iostream>
using namespace std;
#define MOD 10

// declaration ***
int f(int x);
int q(int x);
int g(int x);
int h(int x);
// **************

// main ***
int main() {

    cout << g(4);

    return 0;
}
// **************

// definition ***
int f(int x){
    if(x == 4) return 1;
    return 3*f(((2*x+2)%11)-1);
}

int q(int x){
    if(x == 7) return 1;
    return f(x) + q((3*x)%MOD);
}

int g(int x){
    if(x == 0) return 1;
    return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}

int h(int x){
    if(x == 0) return 1;
    return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}
// **************

Also do not include #include bits/stdc++.h

RoQuOTriX
  • 2,871
  • 14
  • 25
1

Declare your functions first.

#include<bits/stdc++.h>
#define MOD 10

// declaring your functions here makes sure that you can
// use them before they are fully defined.
int f(int x);
int q(int x);
int g(int x);
int h(int x);

// Now here below you can use the functions declared above
// at any place you wish.
int f(int x){
    if(x == 4) return 1;
    return 3*f(((2*x+2)%11)-1);
}


int q(int x){
    if(x == 7) return 1;
    return f(x) + q((3*x)%MOD);
}

int g(int x){
    if(x == 0) return 1;
    return (g(x-1)+f(g(x-1)) + f(x-1) + h(x-1))%MOD;
}

int h(int x){
    if(x == 0) return 1;
    return (2*h(x-1) + g(x-1) + q(f(x-1)%10))%MOD;
}


int main() {

    std::cout << g(4);

    return 0;
}
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1

You need to add a forward declaration of function h in order for your code to compile, something like:

#define MOD 10

int f(int x){
    if(x == 4) return 1;
    return 3*f(((2*x+2)%11)-1);
}

int h(int); // add this line and you will be alright! ;)

int q(int x){
    if(x == 7) return 1;
    return f(x) + q((3*x)%MOD);
}

...

Generally you can find out why this is needed here.