Jump to content

[SOLVED] Recursive function with reference parameter


photo

Recommended Posts

Hi !

I have a problem with a recursive function that receive an integer as reference parameter.

I increment this integer in the function then when I return, I'm expecting the value to be incremented but no, it's the old value.

When I call an other function (not recursive), it works ok the value after the return is incremented.

 

 

A sample code :

 

'foo' is the recursive function

'bar' is an other function

 

Look at the 'After recurse' log line

void foo(int &a) { 

    if (a == 5)
        return;

    log.message("Start : %d",a);
    a++;
    log.message("Incremented : %d",a);

    foo(a);

    log.message("After recurse : %d",a);

    if( a == 1){

        bar(a);
        log.message("After Bar incremented : %d", a);
    }
}
void bar(int &a) {

    log.message("Bar : %d", a);

    a++;

    log.message("Bar incremented : %d", a);
}
 
Output :
 
10:52:59 Start : 0
10:52:59 Incremented : 1
10:52:59 Start : 1
10:52:59 Incremented : 2
10:52:59 Start : 2
10:52:59 Incremented : 3
10:52:59 Start : 3
10:52:59 Incremented : 4
10:52:59 Start : 4
10:52:59 Incremented : 5
10:52:59 After recurse : 5
10:52:59 After recurse : 4
10:52:59 After recurse : 3
10:52:59 After recurse : 2
10:52:59 After recurse : 1
10:52:59 Bar : 1
10:52:59 Bar incremented : 2
10:52:59 After Bar incremented : 2
 
Expected output :
 
10:52:59 Start : 0
10:52:59 Incremented : 1
10:52:59 Start : 1
10:52:59 Incremented : 2
10:52:59 Start : 2
10:52:59 Incremented : 3
10:52:59 Start : 3
10:52:59 Incremented : 4
10:52:59 Start : 4
10:52:59 Incremented : 5
10:52:59 After recurse : 5
10:52:59 After recurse : 5
10:52:59 After recurse : 5
10:52:59 After recurse : 5
10:52:59 After recurse : 5

 

Thanks for your help

 

 

 

Link to comment

Forgot to tell that I call only foo() like this :

 

    int i=0;

    foo(i);

 

But I have some update :

 

Passing a class as reference to a recursive function works great ...

I've implemented a class like that one :

	class Idx{

		int m_i;

		Idx(){

			m_i = 0;
		}

		Idx(int i){

			m_i = i;
		}

		void inc(){

			m_i = m_i+1;
		}

		void inc(int i){

			m_i = m_i + 1;
		}

		int val(){

			return m_i;
		}
	};

I've replaced my int by that class and it works great.

That's a work around but I think that the posibility to use primitive types is better.

Any idea ?

Link to comment

Hi,

 

Thank you for this information! This issue was passed to the developers. I'm afraid at this moment issue have lowest priority.

 

If it acceptable in your case, you can use simple workaround to achieve correct results, but without passing argument as reference:

int a;
void foo() {
	if(a == 5) return;
	
	log.message("Start : %d\n",a);
	a++;
	log.message("Incremented : %d\n",a);
	
	foo();
	log.message("After recurse : %d\n",a);
	
	if(a == 1) {
		bar();
		log.message("After Bar incremented : %d\n",a);
	}
}
void bar() {
	log.message("Bar : %d\n",a);
	a++;
	log.message("Bar incremented : %d\n",a);
}
int a = 0;
	foo();

Sorry for the inconvenience caused.

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment

 

I'm afraid at this moment issue have lowest priority.
 
Wow, a fundamental UNIGINE script language feature bug having the potential to screw all kinds of customer code calculations = lowest priority ????!!?? 
 
Sorry to say that, but you really should rethink this. Such kind of fundamental errors ( a+1 = a, that's what we are facing here...) ALWAYS must have the highest priority or something is wrong with prioritization.... 
Link to comment
×
×
  • Create New...