Functions

Started by
15 comments, last by SteveBe 21 years, 9 months ago
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?
Advertisement
Is it a global variable? Also, it''s .6354f not .6354
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
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.
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.
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????
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.

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);
But the function returns usermoney!
Call it like so...

money = checkwinnings(wincode, money) ;

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
Feel free to email me at NYYanks432@hotmail.com if you have any questions
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

GCoder

This topic is closed to new replies.

Advertisement