Home > Software engineering >  C Calling a function inside a function
C Calling a function inside a function

Time:10-31

I try to call a function in secim() because I want to shorten this function, but it gives a c3861 error. I try a lot of things, but every time it gives a different error. I thought it would be best to share the function without splitting it, because I don't know which way is true. I am new to programming, I think it's an easy problem, but I can't solve it.

#include <iostream>

using namespace std;

int fact(int n) { // function to calculate factorial of a number
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}


int npr(int n, int r) { // finding permutation
    int pnr = fact(n) / fact(n - r);
    return pnr;
}

int combin(int n, int r)
{
    int f1, f2, f3, y;
    f1 = fact(n);
    f2 = fact(r);
    f3 = fact(n - r);
    y = f1 / (f2 * f3);
    return y;
}

int secimm2(int s, int n, int r)
{
    int ss;
    cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
    cin >> ss;
    if (ss == 1) {
        return secim();
    }
    else {
        return 0;
    }
}

int secim()
{
    int s, n, r;
    
    cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
    cin >> s;
    if (s == 1) {
        cout << "1 adet sayi girin\ " << endl;
        cin >> n;
        cout << fact(n) << endl;
        return secimm2(s,n,r);
    }
    else if (s == 2) {
        cout << "2 adet sayi girin\n ";
        cin >> n;
        cin >> r;
        cout << npr(n, r) << endl;
        return secimm2(s, n, r);
    }
    else if (s == 3) {
        cout << "2 adet sayi girin\n ";
        cin >> n;
        cin >> r;
        cout << combin(n, r) << endl;
        return secimm2(s, n, r);
    }
    else {
        cout << "hatali giris tekrar dene\n" << endl;
        return secimm2(s, n, r);
    }
}

int main() 
{
    int s;
    cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
    cin >> s;
    cout << secim();
}

CodePudding user response:

Error C3861 is "identifier not found". The problem is that secimm2() is trying to call secim() before it has been declared, so the compiler doesn't know what secim() is. If you move the implementation of secim() above secimm2(), you would get a similar error about secim() trying to call secimm2() before it is declared.

You need to use a forward declaration to solve this, eg:

int secim(); // <-- add this

int secimm2(int s, int n, int r)
{
    ...
    return secim();
    ...
}

int secim()
{
    ...
}

CodePudding user response:

The main issue (after you edited your question) is that the identifier secim has not yet been declared when it is first encountered (in the function secimm2). The normal way to get around this type of issue is by declaring function prototypes.

However, that's not appropriate here. You are using recursion to control what's actually a basic looping construct. Change your "continue" function as follows:

int secimm2()
{
    int ss;
    cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
    return (cin >> ss && ss == 1) ? 1 : 0;
}

That will return 0 unless a value of 1 is successfully read.

Then in your main, you can just loop until secim returns zero:

int main() 
{
   while (secim() != 0);
}

CodePudding user response:

The compilator reads your code from top to bottom and will complain if it sees a function that isn't defined.

For example:

void foo() {
    bar();    // Error: What is bar()? Not declared yet.
}

void bar() {
    foo();    // OK: foo was declared and defined above.
}

The solution is to declare the function causing the issue before calling it:

void bar();   // Tells that a method void bar() exists. But doesn't define it yet.

void foo() {
    bar();    // OK: we know what bar() is: a call to the void bar() function.
}

void bar() {  // Bar is now defined. Signature must follow declaration.
    foo();    // OK: foo was declared and defined above.
}

With your code, it gives the following fix:

#include <iostream>

using namespace std;

int secim(); // Declares the secim function, but doesn't define it.

int fact(int n) { // function to calculate factorial of a number
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}


int npr(int n, int r) { // finding permutation
    int pnr = fact(n) / fact(n - r);
    return pnr;
}

int combin(int n, int r)
{
    int f1, f2, f3, y;
    f1 = fact(n);
    f2 = fact(r);
    f3 = fact(n - r);
    y = f1 / (f2 * f3);
    return y;
}

int secimm2(int s, int n, int r)
{
    int ss;
    cout << "yeniden denemek ister misiniz 1-evet 2-hayir" << endl;
    cin >> ss;
    if (ss == 1) {
        return secim();
    }
    else {
        return 0;
    }
}

int secim()
{
    int s, n, r;
    
    cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
    cin >> s;
    if (s == 1) {
        cout << "1 adet sayi girin\ " << endl;
        cin >> n;
        cout << fact(n) << endl;
        return secimm2(s,n,r);
    }
    else if (s == 2) {
        cout << "2 adet sayi girin\n ";
        cin >> n;
        cin >> r;
        cout << npr(n, r) << endl;
        return secimm2(s, n, r);
    }
    else if (s == 3) {
        cout << "2 adet sayi girin\n ";
        cin >> n;
        cin >> r;
        cout << combin(n, r) << endl;
        return secimm2(s, n, r);
    }
    else {
        cout << "hatali giris tekrar dene\n" << endl;
        return secimm2(s, n, r);
    }
}

int main() 
{
    int s;
    cout << "islem seciniz\n1-faktoriyel\n2-perm\n3-kombinasyon\n";
    cin >> s;
    cout << secim();
}
  • Related