Calling Another Function in a Recusive Function results in error. Why?

时间秒杀一切 提交于 2020-12-15 19:17:52

问题


I've two functions fun() and fun2();

fun() calls itself while incrementing the global variable a=0 and terminates if a==5. But if I call another function called fun2() which basically returns and do nothing, then Build Error happens. Why? I'm just experimenting with recursion and I got this error.

#include<iostream>
using namespace std;
int a = 0;

void fun() {
    if (a == 5)
        return;
    a++;
    fun();
    fun2();  //This is where the trouble happens
}
        
void fun2() {
    return;
}

int main() {
    fun();
    return 0;
}

Output:


回答1:


You would get similar error with

void fun() {
    fun2(); 
}

void fun2() {}

int main() {
    fun();
}

The error is not related to the recursion. You need to delcare a function before you can call it:

#include<iostream>

void fun2();   // declaration

void fun() {
    // ...
    fun2();  
}

void fun2() { }  // definition

int main() {
    fun();

    return 0;
}

...or simply define fun2 before fun.




回答2:


You must declare or define functions before using them.

#include<iostream>
using namespace std;
int a = 0;
void fun2(); // add this (declaration of function fun2)
void fun() {
    if (a == 5)
        return;
    a++;
    fun();
    fun2();  //This is where the trouble happens
}

void fun2() {
    return;
}
int main() {
    fun();

    return 0;
}



回答3:


"why does that?"

Well, not because of recursion.

In this case, it does this partly because your code is c-style, which mostly does require declaration of user defined functions that your code calls.


You tagged this post with c++, but use not one class.

And, for example, if these functions were encapsulated in a class, then the c++ compiler will (usually) find the functions ... even with no prior declaration.

Example:

In the following, I have encapsulated your code inside a functor (a simplified c++ class)

And, because your code does no output, I've added cout's at locations to demonstrate, visually, that the code compiles and the functions are executed.

#include <iostream>
using std::cout, std::cerr, std::endl, std::hex, std::dec, std::cin; 

#include <string>
using std::string, std::to_string;



int a = 0;  // still global, though many discourage use


// functor ... a simple class
class F705_t // ctor and dtor are compiler provided defaults
{
public:

// main() enters at this function
int operator()(int argc, char* argv[]) 
{
  return exec(argc, argv);  // exec not yet declared!
}

private:

int exec(int , char** ) // line 31
{
  cout << "\n  F705_t::exec()";
  fun();   // using fun() before declared
  cout << endl;
  return 0;
}

void fun() // line 37
{
  cout << "\n  F705_t::fun()" << "  a: " << a;

  if (a == 5)
    return;
  a++;
  fun();   // recursive call 
  fun2();  // using fun2() before declared
}
        
void fun2()  // line 47
{
  cout << "\n  F705_t::fun2()" << "  a: " << a;;
  return; 
}
} // class F705_t

// main() is a c-ism, 
// and is required of the user in most (but not all) c++ environments
int main(int argc, char* argv[]) { return F705_t()(argc, argv); }
//           how invoke a functor --------^^^^^^^^^^^^^^^^^^^^

Output:

  F705_t::exec()
  F705_t::fun()  a: 0
  F705_t::fun()  a: 1
  F705_t::fun()  a: 2
  F705_t::fun()  a: 3
  F705_t::fun()  a: 4
  F705_t::fun()  a: 5
  F705_t::fun2()  a: 5
  F705_t::fun2()  a: 5
  F705_t::fun2()  a: 5
  F705_t::fun2()  a: 5
  F705_t::fun2()  a: 5


来源:https://stackoverflow.com/questions/64209188/calling-another-function-in-a-recusive-function-results-in-error-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!