HOWTO: return a variable from a void function?

Hi,

Im working on a really simple RPG at the moment. This is one of the functions i made:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Attackfunc()
{
	int nRand = Random(1,100);
	int nAtt = 0;
	int nMissCap = nAgiM + nAgiP;

	if (nRand <= nAgiP)
	{
		nAtt = Attackcrit();
		cout << "Player hits Monster for " << nAtt << " damage. (Crit!)" << endl;
	}
	else if (nRand >  nAgiP && nRand <= nMissCap)
	{
		nAtt = 0;
		cout << "Player hits Monster for " << nAtt << " damage. (Miss!)" << endl;
	}
	else
	{
		nAtt = Attacknormal();
		cout << "Player hits Monster for " << nAtt << "." << endl;
	}

}


For some other functions and the mainprogram i need the value of the nAtt variable. Can someone explain to me how i can make this variable accessable (public?).

TIA!
Last edited on
1
2
3
4
int Attackfunc() {
 int nAtt = 3;
 return nAtt;
}


Or pass in the address of an int that the function sets to the value of nAtt

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 calling_func()
{
	int returned_nAtt;
	Attackfunc(&returned_nAtt);
	// reurned_nAtt is now set to the value of nAtt
}

void Attackfunc(int *aAtt_p)
{
	int nRand = Random(1,100);
	int nAtt = 0;
	int nMissCap = nAgiM + nAgiP;

	if (nRand <= nAgiP)
	{
		nAtt = Attackcrit();
		cout << "Player hits Monster for " << nAtt << " damage. (Crit!)" << endl;
	}
	else if (nRand >  nAgiP && nRand <= nMissCap)
	{
		nAtt = 0;
		cout << "Player hits Monster for " << nAtt << " damage. (Miss!)" << endl;
	}
	else
	{
		nAtt = Attacknormal();
		cout << "Player hits Monster for " << nAtt << "." << endl;
	}

	*aAtt_p = nAtt;
}

Zaita: I know you can change the function from void to int and add:
 
return: nAtt;

when i do this however instead of getting just the value when i try to call the function i get the whole function, even when i try assigning it to a variable.

bnbertha:
I think youre using a reference or something similar, as i said i just started programming so i dont know too much about references yet. When i try to compile your code i get this error in line 8:

unresolved external symbol


Thanks for your help.
Last edited on
when i do this however instead of getting just the value when i try to call the function i get the whole function, even when i try assigning it to a variable.


I could not get what problem u r facing when U R using as stated by Zaita?
Well, when i do this:

1
2
3
4
5
6
int Attackfunc()
{
//code

return nAtt;
}



I dont only get the value of nAtt but also the rest of the code, and all i need is the value from nAtt
Last edited on
If I understand correctly, you want to read the value previously produced by the function without calling the function a second time. You can do this by declaring the value outside the function (making it a "global" variable):

1
2
3
4
5
6
7
8
int nAtt = 0;

void Attackfunc()
{
    // Same code as before, but remove the line 'int nAtt = 0'
}

// Access nAtt from anywhere 


EDIT: I actually don't recommend this. I think the previous suggestions are better.
Last edited on
thanks ropez, this works. I understand that global variables should be avoided whenever possible, so id like to know why i get this error:

unresolved external symbol

when i try to use bnbertha's suggestion, any ideas?
The unresolved symbol will be because you have not linked to a library correctly.

I do not understand your issue with my code though?
1
2
3
4
5
6
7
8
9
10
11
12
13
14

int Attackfunc()
{
//code

return nAtt;
}

int main() {
 int value = Attackfunc();
 
 cout << "Value is: " << value << endl;

}


??
I suspect the problem with my code is that Attackfunc() is used before it is declared or defined. Basically the compiler won't let you call a function it doesn't know about, and it's knowledge is strictly in top to bottom file order. If this is the problem, to fix it either declare a prototype of Attackfunc() before calling_func() or swap the functions around so that Attackfunc() is before calling_func() in the file. The prototype declaration would be a single line as follows
 
void Attackfunc(int *aAtt_p);


Briefly, the technique I have used is a pointer, a word that unnecessarily strikes fear into most people starting to learn C/C++. They are very similar to the reference operator however the syntax is slightly different. Look here for a much better explanation than I can give; http://www.cplusplus.com/doc/tutorial/pointers.html.
I think either approach would work equally well. I do question someone's ability to use a pointer correctly when they are having issues with a simple return-type function though. This maybe adding unnecessary complexity to the situation. Especially as they are copying+pasting your code without understanding why a reference is used. Don't you agree bnbertha?

Last edited on
@ Zaita:
If it was me writing the code, in this particular case I'd probable return an int rather than pass a pointer. However as soon as you want more than one value you're into pointers (and/or structures) and I think it's better to get your head around them with something like this than when you have no choice but to use them. So yes I do agree but providing simple ( I hope this one is) examples to see how they work together with reading the reference may nudge someone into trying to understand. You can only point people in the right direction.....
Last edited on
Completely agree :) In this case yea a int f(); approach would be more than sufficient.
Topic archived. No new replies allowed.