function

hello i have question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void lose()
{
    cout<<"Game over! You lose";
    cout<<endl;
    if(exp>=50)
    {
        cout<<"You exp has been down into "<<exp-50;
    }
    else
    {
      cout<<"You exp has been down into "<<50-exp;   
    }
    
}
int experience(int exp=0)
{
    if(win)
    {
       exp=exp+20;
       if(exp == 100)
       {
           lvl=lvl+1;
           if(true)
           {
               exp=0;
           }
       }
    }
}


in the first function the exp was na declared in the scope but i dont know how to fix it haha. should i global the variable exp ? instead of putting it in experience function?
Last edited on
also but if i global the exp then if i call the experience it wont work the way it should be
i fixed it now
is this right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void lose()
{
   int exp_down;
    cout<<"Game over! You lose";
    cout<<endl;
    if(exp_down>=50)
    {
        cout<<"You exp has been down into "<<exp_down-50;
    }
    else
    {
      cout<<"You exp has been down into "<<50-exp_down;
    }
 experience(exp_down);
}
int experience(int exp)
{
    int exp_=0;
    if(win)
    {
       exp=exp+20;
       if(exp == 100)
       {
           lvl=lvl+1;
           if(true)
           {
               exp=0;
           }
       }
    }
}
Why not pass the variable exp as a parameter?
 
void lose(int exp) 

or if the function needs to change the value of exp, pass it by reference,
 
void lose(int & exp)
i never learn about void lose (int exp)

is the pass by parameter is another way to call the function just like in my situation?
Line 6: emp_down is uninitialized variable. You're going to be comparing garbage.

Line 16: What's the point of exp_? You don't use it.

Line 19: win is undefined.

Line 24: lvl is undefined.

Liness 16-31: You change exp, but those changes are lost when you exit experience().

i never learn about void lose (int exp)

Then read about functions:
http://www.cplusplus.com/doc/tutorial/functions/

is the pass by parameter is another way to call the function just like in my situation?

Passing by value and by reference is explained in the above link also.

@AbstractionAnon
yeah i forgot to initialize exp_down
line 16 is a typo, ignore exp_
lvl19: win is defined this is just some part of my code
same with line 24


thank you im going to fix 16-31 myself.
i already read it like a million times i still dont understand haha. a little bit
but this is my first time to code.

but now, this fix my code thank you all :)
i already read it like a million times

I think the way to approach that page is to focus closely on the sample code. Copy it, try to compile and run it. When you see what it actually does, go back and re-read the explanation.

In addition, you could take the sample code and play around with it, if you think you understand it, change some part and see whether or not it behaves as expected. Gradually, bit by bit you build up knowledge by a combination of both reading and doing.
also @AbstractionAnon im about to go to bed but i realized about what you did say
Liness 16-31: You change exp, but those changes are lost when you exit experience()
.

what if i call the function experience in main function every time i win? would it still be lost?
@Chervil

yeah i can see what you are saying. but its the one and only one i dont know why i cant understand it.. but thanks for your advice gonna credit you when i already did it :) thank you
what if i call the function experience in main function every time i win? would it still be lost?

Yes. It doesn't matter where you call experience() from. experience() never communicates the new value of exp back to the caller.

There are two ways to do this.
1) Make experience() and int function and have it return the new value of exp. The caller is then responsible for updating his copy of exp with the returned value.
2) Pass exp by reference to experience(). This allows experience() to update the caller's variable.


1) Make experience() and int function and have it return the new value of exp. The caller is then responsible for updating his copy of exp with the returned value


o yah yah.. i forgot that i used int in function which the main expecting to return a value
2) Pass exp by reference to experience(). This allows experience() to update the caller's variable.

i understand.

another question would the void lose function still work properly? look, i call the experience function inside the void which will not return value or should i really have to pass it by reference ?
If you take approach #1, every function called needs to return exp back to the point where the original definition of exp is (presumably main).

Return by value is fine where a function returns a simple value such as rand() or sqrt(), etc. Return by value gets ugly when a called function wants to update more than one value in the caller. This is where pass by reference (#2) is preferred. If you need to update more than one value in the caller, you can simply add another pass by reference argument to your function.
Topic archived. No new replies allowed.