PDA

View Full Version : C++ Function



Fishy
18th January 2006, 01:32
int Fight (int variable)
{
blah;
return 0;
}
int main()
{
int damage;
damage=Fight(variables);
system ("PAUSE");
return 0;
}


Errors:
In function int main(int, char**)
Expected primary-expression before 'int'

Im just trying to make it run the fight function and return back a variable to damage. But i got those errors any help please.

O_O
18th January 2006, 01:56
int variables = 2; // as example
damage=Fight(variables); // then it will be right... 'cause your Fight func takes int, but you didn't create it before

and in func change this one: blah; :)

Fishy
18th January 2006, 02:01
int variables = 2; // as example


Well the variables i stuck in there is actually a long list that all has been declared and set to a certian value.

Nevermind i got it thanks for the help man....

Anddos
19th January 2006, 02:19
when you make a function in c++ , you must prototype it unless its done in a class or struct.
example

int Fight(int varible); //always do a first copy with ; at the end so the compiler knows about the function

int Fight (int variable)
{
return variable;
}

int main()
{
int damage;
damage=Fight(5);
system ("PAUSE");
return 0;
}

Enviro
19th January 2006, 18:12
Actually, as long as the function is completely written before being called you don't have to prototype it. Just like his code shows, Fight doesn't have to be prototyped because it is defined before it is called in main.