Page 1 of 1
Method();
Posted:
Sun Oct 16, 2011 11:23 am
by Troodon
How can I build and call methods with parameters?
Like, I code a method
listTopPlayers(int myVariable)
{
//the method's code where it uses the variable myVariable
}
and then whenever I want to use the tool, I just call it somehow, like listTopPlayers(10);
So that I don't need to write the code again everytime when I need it. Or if I want to change the tool, I don't have to change it everywhere in the code.
Thanks
Re: Method();
Posted:
Sun Oct 16, 2011 11:29 am
by skydereign
You mean functions? You declare them in global code using this format.
Global Code
- Code: Select all
return_type
function_name (argument list...)
{
// code
}
So for your function you'd probably set return_type to void.
- Code: Select all
void
listTopPlayers (int myVariable)
{
// code
}
Then call the function whenever.
Re: Method();
Posted:
Sun Oct 16, 2011 11:34 am
by Troodon
Thanks, that looks right. How do I call the function?
function_name(argument); ?
Re: Method();
Posted:
Sun Oct 16, 2011 11:35 am
by Game A Gogo
Exactly!
Re: Method();
Posted:
Sun Oct 16, 2011 12:01 pm
by Troodon
Thank you!
Re: Method();
Posted:
Sun Oct 16, 2011 12:08 pm
by lcl
You may find this useful, a tutorial of functions:
viewtopic.php?f=4&t=10848
Re: Method();
Posted:
Sun Oct 16, 2011 1:57 pm
by Troodon
Kiitän.
Still one thing, can I overload the function to do different things with different parameters?
I mean different depending if the user puts:
function_name();
function_name(argument);
function_name(argument,anotherArgument);
Re: Method();
Posted:
Sun Oct 16, 2011 3:36 pm
by Game A Gogo
unfortunately no, the parameters you put in your function declaration always has to be present when you use them
Re: Method();
Posted:
Sun Oct 16, 2011 3:40 pm
by Troodon
Ok. Well it's good enough anyway.
Cool that this community is still active enough to give answers the same day one asks a question!
Re: Method();
Posted:
Sun Oct 16, 2011 8:04 pm
by Game A Gogo
well we try! It's all about collective effort, or something