Jump to content
  • Advertisement

Archived

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

SteveBe

Functions

This topic is 5835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Andrew - why does it have to have f after the number? I thought that was entirely optional. My program will calculate correctly without it won''t it????

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!