For loop inside recursion?

Ive been trying to understand this recursion I got from a tutorial.

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
	void processNode(aiNode* node, const aiScene* scene)
	{
		//std::cout << "this is the mother " << node->mNumMeshes << std::endl;
		// Process each mesh located at the current node


		for (GLuint i = 0; i < node->mNumMeshes; i++)
		{

			std::cout << " Call the meshes " << node->mNumMeshes << std::endl;
			// The node object only contains indices to index the actual objects in the scene. 
			// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
			aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
			this->meshes.push_back(this->processMesh(mesh, scene));
		}
		
			
		for (GLuint i = 0; i < node->mNumChildren; i++)
		{
  			std::cout << " i variable is " <<i << std::endl;
			std::cout << " Call the Children "  << node->mChildren[i] <<  std::endl;
			
			this->processNode(node->mChildren[i], scene);
		}

	}


This code uses assimp which will the model's vertices textures and indices. I was able to simplify it using just for oop but something is a bit strange on this recursion. The Gluint i on the for loop doesnt return to zero. here is the result. you will see that the i variable just keep on incrementing. I was really expecting the I to return to zero

[url=http://postimg.org/image/44ntwdksd/][img]http://s22.postimg.org/44ntwdksd/recursion.jpg[/img][/url]
Last edited on
Looks like the root node has 7 children but none of the root's children have any children.
yes but Im really confuse how does that i variable in here

GLuint i = 0; i < node->mNumChildren; i++
retain its value after calling the function itself again? That should be set to zero again. I dont understand? HOw is that posible?
Last edited on
It's not the same variable. Each function call gets its own copy of i.
I see. I tried replicating it on my own understanding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void recursion()
{
	for (int i = 0; i < 10; i++)
	{
		std::cout << " i variable is " << i << std::endl;
		recursion();
	}
}


int main()
{
	
	recursion();

	return 0;
}


This time the i variable returns 0 and doesnt even increment. So uhm if every function call has their own copy of i then that i on the code above should increment same as the code on my first post. Really confuse now..
Only i=0 gets printed because the function never returns. It calls itself recursively infinitely.

1
2
3
4
5
6
7
8
9
10
recursion()
	print i=0
	recursion()
		print i=0
		recursion()
			print i=0
			recursion()
				print i=0
				recursion()
					...
Last edited on
I really dont get it. Its the for loop that really bugs me. Its just I really dont get how and why. I cant think of any simple program that would mimic the same output on my first post.

I dont see the connection of i variable on the function argument. I mean I pass the child node yes but that is not related to the declared i variable on the scope of the for loop.

This should be

1
2
3
4
5
6
recursion...
    for loop(i = 0)...
       recusion(child node)... 
           for loop(i = 0)...
                 recusion(child node)... 
                       for loop(i = 0)...

The for loop of the recursion() function could be removed because it never returns from the function call so it never gets past the the first iteration.

1
2
3
4
5
void recursion()
{
	std::cout << " i variable is " << 0 << std::endl;
	recursion();
}

recursion() calls recursion() which calls recursion() which calls recursion() ...

As you can see this recursion never ends. A recursive function needs a condition where the recursion stops. In the processNode function the recursion stops when a node does not have any children.

Take a look at the following program instead. It clearly shows that the i variable is not affected by the recursive call.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

void recursion(int depth, int maxDepth)
{
	for (int i = 0; i < 3; i++)
	{
		std::cout << std::string(depth - 1, '\t') 
		          << " i variable is " << i << std::endl;
		if (depth < maxDepth)
		{
			recursion(depth + 1, maxDepth);
		}
	}
}

int main()
{
	recursion(1, 3);

	return 0;
}
Last edited on
Topic archived. No new replies allowed.