449,110 Members | 1,010 Online
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

# recursion order question

 P: n/a hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to call it in a specific order. in a simple way : evaluating some expression in its original order see the following part of code: class retType { .... retType doSomething(someType4,retType); .... }; retType recFun(someType3 ,someType1) { .... someType1 b,d; someType2 c; someType3 a; .... return recFun(a,b).doSomething(c,recFun(a,c)) .... } well , i was sure (for some weird reason...) that the first call will be recFun(a,b) but actually the first call was recFun(a,c) . the problem is that i wanted a specific order (first first then the second .. obviously..) and it wont do it. some of my limitations are: i dont want to keep any copies of the results of the calls ( it creates more nodes in some tree ... which obviously i dont want to count) and the second is that i need the keep the order due some other dependencies . so any ideas/revelations will be most welcomed . bye lig p.s. this is my first post on this kind of groups so excuse me if i dont meet the protocol rules ... Sep 15 '05 #1
6 Replies

 P: n/a lig wrote: hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to call it in a specific order. in a simple way : evaluating some expression in its original order see the following part of code: class retType { ... retType doSomething(someType4,retType); ... }; retType recFun(someType3 ,someType1) { ... someType1 b,d; someType2 c; someType3 a; ... return recFun(a,b).doSomething(c,recFun(a,c)) ... } well , i was sure (for some weird reason...) that the first call will be recFun(a,b) but actually the first call was recFun(a,c) . The order is undefined, a compiler could do either, and both would be right. the problem is that i wanted a specific order (first first then the second .. obviously..) and it wont do it. some of my limitations are: i dont want to keep any copies of the results of the calls ( it creates more nodes in some tree ... which obviously i dont want to count) Maybe you can use a reference const retType& tmp = recFun(a,b); return tmp.doSomething(c,recFun(a,c)); but you would have to change the function signature to be const. and the second is that i need the keep the order due some other dependencies . I'd seriously look at relaxing that restriction. so any ideas/revelations will be most welcomed . bye lig john Sep 15 '05 #2

 P: n/a "lig" wrote in message news:11**********************@g14g2000cwa.googlegr oups.com... hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to call it in a specific order. in a simple way : evaluating some expression in its original order see the following part of code: class retType { ... retType doSomething(someType4,retType); ... }; retType recFun(someType3 ,someType1) Are someType3 and someType1 classes? Are they defined somewhere? Why don't you have names for them here? Aren't you planning on using them inside the function somewhere? (Also, are you sure you want to pass those by value?) { ... someType1 b,d; someType2 c; someType3 a; These are local variables. What about the parameters that were passed in. As it is, you're ignoring them entirely. ... return recFun(a,b).doSomething(c,recFun(a,c)) You've said above that c is of the type "someType2", but you're passing as the first parameter to doSomething, which expects a variable of type "someType4". You're also passing it as the second parameter of recFun, which is expecting a "someType1" variable. ... } well , i was sure (for some weird reason...) that the first call will be recFun(a,b) but actually the first call was recFun(a,c) . How else could it work? It has to pass a retType variable to doSomething. In order to do that, it first needs to call recFun, in order to get back the retType result, right? So, before it can call doSomething, it has to execute recFun. the problem is that i wanted a specific order (first first then the second .. obviously..) and it wont do it. some of my limitations are: i dont want to keep any copies of the results of the calls ( it creates more nodes in some tree ... which obviously i dont want to count) If you don't want copies, then you probably don't want to pass and return by value. But what you _do_ want is really impossible for me to tell, because your code doesn't really make sense. (And how does doSomething create a retType object. You left out that code.) and the second is that i need the keep the order due some other dependencies . I'm not sure what order it is you need to keep. But there's no way to execute doSomething unless you give it the values it needs, which means you have to somehow compute those values first (unless doSomething itself can make a function call to get the value it needs, instead of passing it that second parameter). -Howard Sep 15 '05 #3

 P: n/a "Howard" wrote in message news:7LiWe.45855\$qY1.21373@bgtnsc04- well , i was sure (for some weird reason...) that the first call will be recFun(a,b) but actually the first call was recFun(a,c) . How else could it work? It has to pass a retType variable to doSomething. In order to do that, it first needs to call recFun, in order to get back the retType result, right? So, before it can call doSomething, it has to execute recFun. Oops, my mistake. You were talking about the two calls to recFun, not the call to doSomething. As John stated, the order of those calls is not defined. But you can put them in separate statements like he showed to control the order. -Howard Sep 15 '05 #4

 P: n/a On Thu, 15 Sep 2005 18:01:18 GMT, John Harrison wrote in comp.lang.c++: lig wrote: [snip] return recFun(a,b).doSomething(c,recFun(a,c)) ... } well , i was sure (for some weird reason...) that the first call will be recFun(a,b) but actually the first call was recFun(a,c) . The order is undefined, a compiler could do either, and both would be right. ITYM "unspecified", not "undefined". A world of difference. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html Sep 16 '05 #5

 P: n/a Jack Klein wrote: On Thu, 15 Sep 2005 18:01:18 GMT, John Harrison wrote in comp.lang.c++:lig wrote: [snip] return recFun(a,b).doSomething(c,recFun(a,c))...}well , i was sure (for some weird reason...) that the first callwill be recFun(a,b) but actually the first call was recFun(a,c) .The order is undefined, a compiler could do either, and both would be right. ITYM "unspecified", not "undefined". A world of difference. Right, in other words it is one order or the other. john Sep 16 '05 #6

 P: n/a first thanks for the ideas . second , yea i did a type mistake there above ... (tried to keep it simple) i'll think i'll try the local copy option. i missed the part that the dtor of retType class (which i didnt wrote myself) is removing his projection from the tree , so the limitation of not keeping copies is no longer relevant. thanks again for your ideas. lig Sep 20 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.