0

can you call a function inside that same function so that it goes to the beginning of that code to execute it. So even though ur already in the prompt function can u still call it again from withing the code inside the prompt function. Thanks in advance.

5
Contributors
6
Replies
16
Views
8 Years
Discussion Span
Last Post by akshaykeerthi.srikanth
0

>can you call a function inside that same function
If I understood that correctly then..

If you call a function while your already in it, you have recursion. Take this as an example.

#include <iostream>
using namespace std;

void CountDown(int number) {
   if (number != 0) {
      cout << number << '\n';
      CountDown(--number);
   }
}

int main() {
   CountDown(100);
   cin.ignore();
   return 0;
}

This will keep calling the CountDown function, subtracting 1 from that number, and then displaying it until number reaches 0, when that happends, it will stop calling CountDown and it will break out the loop. Here is a tutorial on recursion - http://www.cprogramming.com/tutorial/lesson16.html

Hope this is what your looking for ;)

0

Of course, you can do that. It's called recursion. The only problem is to break recursive call chain in the proper moment. In other words you must prevent f() call which calling f() which calling f() again and so on...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.