   Question on Return Types of Functions
  Heyas,The way I have my current game set up, it''s an RPG battle engine type thing, is I have the main CGame class which calls the CPlayer and CMonster classes and goes from there.  However, I am having problems where certain functions CMonster need to call a function from CPlayer, and the other way around.  Which leads to problems when I compile.So, I thought a work around would be to have a CBattle that would be the head of the battle.  When it is the player''s turn, it will call the PlayerTurn() function, which will do it''s thing and sent a return intiger, which the CBattle class will get and then do something based on that return.  The only problem with this is I read that it is bad practice to have mutliple return points in a function.  So I figured I''d have a local int that I would change through the function and at the end return it.  So there would only be one return point, however that single variable will the return value.  As long as I put in some protectoin to that once the return value has been discovered, it cannot be changed, would this be a reasonable way to go about this?  Is having a variable dedicated to  be the return bad practice at all?Thanks,Chris 
quote:Original post by Chryzmo  As long as I put in some protectoin to that once the return value has been discovered, it cannot be changed, would this be a reasonable way to go about this?  Is having a variable dedicated to  be the return bad practice at all?It''s reasonable, and consistently returning the same variable allows compilers to do Named Return Value Optimisation.However, there is nothing wrong with having multiple return statements in a function.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);quote:Original post by Fruny  However, there is nothing wrong with having multiple return statements in a function.   hehe, tell that to my old programming lecturer, I used two returns from a function once (short-cut''d out at the top) and she went mental, ended up having a half an hour arguement/discussion about it, in the end i backed down because i knew she''d mark me down on it, but i knew morally i was right ;  I''m lazy, so I''m going to copy a post I made about this same topic from a different  threadMultiple returns is a worse practice in C than it is in C++. In C++ you can use RAII to guarantee that all acquired resources are released when a function returns, no matter where the return was called. In C, there''s no good (standardized) mechanism to do the same thing. So by obeying SESE you have a better chance of not leaking resources. The same argument can get extended to other languages such as classic Pascal. ;  Thanks for the quick replies.  Now, I have one more quick question.  Say I go the way of having a variable within that function that I will use as the return value.  Would it be better to just do this: int liReturn;//OR this:int *liReturn = new int;//And follow up later with:delete liReturn;Do either of those have advantages over the other.  Or is it more a matter of preference?Thanks,Chrisedit:  typo with the SOURCE tags   [edited by - Chryzmo on February 14, 2004 11:12:53 PM];  What on earth are you trying to accomplish with newing the return value? ;  I''m not sure, I just didn''t know if it would make a differnce if i use new and delete for it, since the variable will only be used briefly within the function or if I should use a normal variable declaraction.  I thought since it was only going to be used briefly rather than for a long time, maybe using new and delete would be better or something. ;quote:Original post by Chryzmo   I thought since it was only going to be used briefly rather than for a long time, maybe using new and delete would be better or something.   Well, the variable needs to exist until you return it. And once you've returned it (assuming you return the value, and not the pointer), it's too late to delete it. Boom, memory leak.Plus you got it backwards, leave transient objects as auto variables, and use new/delete for things that *need* them (hint - return values don't, unless you're actually returning the pointer for later use).   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 14, 2004 11:48:37 PM];  Ah, I see.  Thanks a lot for clearing that up Now, that I think of it, it wouldn''t be much use to delete the variable before I can actually use it 