Could you explain the difference between the execution of code on recursive descent and return (with an example)? How do I determine that the program is running on rivers. descent or return? There is almost no material with examples on the Internet, only more confused. You can see the same algorithm, but written in rivers. descent and return? Thanks for the help, really need to figure it out.

  • 2
    Where you use these terms as “recursive descent”, you look for examples there. :) - Vlad from Moscow
  • The fact is that such a theory was given at the university, but in practice they were not explained and I see no difference. Therefore, I ask for help here. - Maxitt

1 answer 1

An example of a recursive return is the calculating factorial function:

#include <iostream> using namespace std; int fact(int x) { int mult; if (x == 1) mult = 1; else mult = fact(x - 1); // результат вычисляется посредством рекурсивного возврата return mult * x; } int main(int argc, char *argv[]) { int n = 5; cout << "Fact !n = " << fact(n) << endl; return 0; } 

An example of a recursive descent is the directory tree building program.

 #include <iostream> #include <list> using namespace std; list<string> get_subdirs(const string &path) { // какая-то реализация получения списка каталогов, зависимая от ОС } string path_concat(const string &path, const string &name) { // какая-то реализация склеивания пути с именем, напр.: C:\\Users + Admin = C:\\Users\\Admin } string indent(int level) { // сформировать отступ string result; for (size_t i = 0 ; i < level; ++i) result += '-'; return result; } void print_directory(const string &path, int level = 1) { list<string> subdirs = get_subdirs(path); for (list<string>::iterator it = subdirs.begin(); it != subdirs.end(); ++it) { string dir = *it; cout << indent(level) << dir << endl; print_directory(path_concat(path, dir), level + 1); } } int main(int argc, char *argv[]) { print_directory("C:\\"); return 0; } 

The code was written on the knee, I can not guarantee the correctness.

  • I correctly understood that rec.return, this is when some expression is written in the operator return? And the descent is when all actions are performed before return (return will return only the finished result already)? - Maxitt 6:39 pm
  • when the finished result is formed through return and you process it at the starting point - this is a recursive return. When you just come in from yourself to do something this is a descent. - Alexey Malchenko
  • Thanks, now at least something is cleared! - Maxitt