Call the same function, before the first call to it is completed

Hey guys!

I'm trying to call the same function, before the first call to it is completed. My program crashes. Any ideas? And is it possible to make each "recall/callback" unique so they dont affect each others variables inside the function? Maybe thats not the issue here but something is failing, and I dont know what.



Here is a function calling itself before the first call to it is completed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

void function(int value)
{
  if (value == 1) {function(0);}

  cout << value << '\n';
  return;
}
  
int main()
{
  function(1);
}


If your program crashes, it's nothing to do with recursion. It'l be because you're simply doing something dangerous.

And is it possible to make each "recall/callback" unique so they dont affect each others variables inside the function?

Every call to a function is unique. If the functions don't contain any shared values such as statics or globals or some such, then they can't interfere with each other.
Last edited on
Hello Repeater.

Thanks for your reply... The example code below doesn't work with extend_path() inside itself. But when I remove it inside itself, then it works.

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int wordscount = 5; //global

void extend_path(string path) {

	string new_path = "something temp";
	int i = 0;

	while(i < wordscount) {

		extend_path(new_path);

		i++;
	}
}


int main() {

	int i = 0;

	while(i < wordscount) {

		extend_path("something temp");
		i++;

	}

	return 0;

}
Last edited on
Your code makes no sense.
What is it supposed to do? Show example input and output.
You say you have a version that works? Post that, too.
Last edited on
Your code makes no sense.


It does not have to make sens. I want to know why it does not work and crash.

You say you have a version that works? Post that, too.


As i wrote in my comment... The code works when i remove extend_path() inside extend_path()
volang wrote:
It does not have to make sens.

That's very philosophical. But I don't think it will attract many replies.

The code works
Do you mean the code compiles, runs and does ... nothing?

I don't think you will get many answers unless you tell us what is it intended to do?

Just as an aside - your code as posted doesn't compile.
Because your function calls extend_path an infinite number of times, but you don't have infinite memory on your stack so when all the stack is full with a huge number of calls to the function, it crashes.

Calling a function uses some stack space. When the function exits, that stack space is freed. Your code calls extend_path over and over and over, using more and more and more stack, until it's all gone. None of your calls to extend_path will ever finish.
Repeater. Great answer, thank you.

Infinite? But the while loop condition is set to "i < wordscount(5)", it has to eventually stop on all of them?
Last edited on
You call extend_path.

Function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

New call to function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

New call to function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

New call to function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

New call to function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

New call to function extend_path begins.
In that function, i is set to zero.
i is less than wordscount, so this function calls extend_path.

...

The same, forever and ever. When will any one of these function calls finish?
Last edited on
when i is not lesser than wordscount?

wordscount = 5, and i++ is included. So after 5 repeats for every new function call?
i++ is never called. No extend_path function ever gets that far.

Every extend_path function begins with an i value of zero. When will any of them ever reach i++? Never.
Last edited on
Please tell us what the function is supposed to do.
Please tell us what the function is supposed to do.


Nothing brother. This is not the "real world example", but it has the same idea behind it.

i++ is never called. No extend_path function ever gets that far.

Every extend_path function begins with an i value of zero. When will any of them ever reach i++? Never.


You're the man. Thanks for pointing that out (although it should have been obvious to me.) A solution is applied and now it works fine.
Please show us your working code.
Never
Is that because you're a liar and you don't have any "working" code?
Okey
A remarkably restrained finish to this thread, I think. Thanks all.
Topic archived. No new replies allowed.