Archived

This topic is now archived and is closed to further replies.

SteveBe

Functions

Recommended Posts

Small crappy newbie problem here. I''m writing a small "fruit machine" game in DOS whereby the user guesses three numbers and then I use random numbers to simulate the wheels. Once the "wheels" have spun I then check for winnings. I have a variable called usermoney which stores, funnily enough, how much money the user has. I check the winnings in a function called from main. I want to pass usermoney through the checkwinnings function and have it return in its correct state. However for some reason after correctly altering the value in the function body, when I return it, the program reverts it to its previous value. Why? My function prototype is such: float checkwinnings(int wincode, float usermoney); wincode is a variable to help calculate the winnings. The body of the function is then this:- float checkwinnings(int wincode, float usermoney) { switch (wincode) { case 0: printf("\nYou didn''t win anything this time."); break; case 1: printf("\nYou won 10p. Congratulations"); usermoney+=.10; break; case 2: printf("\nYou won 50p. Congratulations"); usermoney+=.50; break; case 3: printf("\nYOU HIT THE JACKPOT! YOU WIN ONE POUND!"); usermoney+=1.00; break; } return usermoney; }; Can someone help?

Share this post


Link to post
Share on other sites
It does this because C, by default, uses something called "pass by value". What that means is that the function operates on a COPY of the value, which is discarded after the function returns.

Read up on pointers if you want to do "pass by reference", which allows you to change the real value from inside the function.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by Andrew Nguyen
Is it a global variable? Also, it''s .6354f not .6354


*** 500 ERROR ***



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by SteveBe
Andrew - why does it have to have f after the number?

Default type for fp literals is double; specifying an ''f'' suffix means the type is float.
quote:

I thought that was entirely optional. My program will calculate correctly without it won''t it????

Your program should work OK, but the compiler will invoke a lossy conversion from double to float, which should really result in a warning. Sometimes the loss of precision might actually be a problem to your program, other times maybe not. Don''t ignore warnings - do something about them! In this case, there are two actions you might take:

1. Use double instead of float;
2. Use the ''f'' suffix.

Number 1 should be the preference on Windows unless you have an overriding reason to prefer float.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just change the function to:
float checkwinnings(int wincode, float *usermoney)
{
//...
}

and (asuming you aren''t using a pointer) call it like:
checkwinnings(wincode, &usermoney);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But the function returns usermoney!
Call it like so...

money = checkwinnings(wincode, money) ;

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
It does this because C, by default, uses something called "pass by value". What that means is that the function operates on a COPY of the value, which is discarded after the function returns.




This is correct. You have to pass by reference to change the value of a parameter. Declare your prototype this way.

float checkwinnings(int wincode, float &usermoney);

(and then when you define the function type that also). You can then call the function the usual way and usermoney will be changed to whatever you set it to in checkwinnings.

The ambersand (&) means it is passing by reference.

When you pass by value, the compiler copies what is stored in the variable to a new location. Then once that function is finished, it discards the variable at the new location and uses the old location with the old value again. However, when you pass by reference, the compiler gives the address of the variable to the new function. So when the new function modifies the variable, it is modifying the variable in both places.

Hope this helped

Share this post


Link to post
Share on other sites
I feel 2 issues are being combined here.

Everything said about "pass by reference" etc is correct, but given the original argument, I feel this is overkill. As the Anonymous Poster said, the function returns the value needed, so all that is needed is to call the functioncorrectly and collect the value returned i.e (as already stated :

money = checkwinnings(wincode, money);

I''m saying all this basically because this is a beginner question and I think explaining the basics of functions is more important at such a stage than explaining (in brief) about passing arguments by reference. Incidentally using the "&" to pass by reference is something I''ve always thought of as ambiguous, especially to newbies. The standard pointer passing is perhaps a little more long-winded, but perhaps less ambiguous considering the broad use of pointers in more complex programs. Of course this is as usual just my opinion. I''m not trying to tell anyone they''re wrong, I just remember what it was like to be a newbie!...

GCoder

Share this post


Link to post
Share on other sites
GCoder, you are right that using the return value is probably simpler.....

For some reason that just seems kind of messy to me. But it probably is better to do that and understand why it is working than to pass an arguement by reference and not understand what is happening.

Share this post


Link to post
Share on other sites
Well guys I took your advice and spent the whole day trying to get my head around pointers and have now successfully finished that function using pointers and have written another couple of functions in the "game" using pointers just to try and get the hang of them.

However, Sabreman - I don''t understand why it is better to use doubles rather than floats. Please explain when I am not using large numbers, but rather small numbers that require a decimal place.

Share this post


Link to post
Share on other sites
There are some advantages to using doubles over floats even with small numbers.

First of all a lot of the functions that are included in standard c++ libraries return doubles rather than floats and it is best to minimize the number of conversions.

Secondly, the differnce between the size of doubles and floats probably will not be an issue b/c of the amount of memory computers have today.

The biggest reason to use doubles IMO is because it will form a good habit.

Doubles are much better because they have extra precision since they are double the size.

There may be other reasons that I''m leaving out.

Share this post


Link to post
Share on other sites
quote:
Original post by SteveBe
So Gamedev would your advice be to ALWAYS use doubles instead of floats???

The typical advice is as I mentioned earlier: double should be the preference on Windows unless you have an overriding reason to prefer float. An overriding reason might be something like you are using OpenGL, which works with floats, or you are using a weird platform which works much better with floats than doubles.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
An overriding reason might be something like you are using OpenGL, which works with floats, or you are using a weird platform which works much better with floats than doubles.

As far as I know, for every OpenGL function there is that works with floats there is an equivalent for doubles. However, floats are notably useful in graphics programming (hence, OpenGL) because they can half the bandwidth for large vertex arrays that must be sent to the video card.

Share this post


Link to post
Share on other sites
I agree with Null and Void. There are certainly times to use floats rather than doubles, especially in 3d design when there could be a lot of vertexes.

However, until you get to very advanced stuff like that, where speed really does become an issue, while I hesitate to say always use doubles, I reccomend in most situations to use a double.

Feel free to email me at NYYanks432@hotmail.com if you have any questions

Share this post


Link to post
Share on other sites