killercoder wrote:I need some help understanding how exactly making your own function works because the book I am reading is making me confused :P
Well, seeing that the other two people who commented on this thought it was a bad idea to post what you did, I guess I must agree with them. How dare you post a topic relevant to the name of the thread! JK, I applaud you for actually putting something relevant, and I am here to help.
So, making your own function... how to explain this. I think i will use java and write a very simple function.
Lets say that you are writing a function whose purpose is to find out if a number provided is between ... two and seventeen? that sounds good.
The function would look something like this...
public boolean twoSeventeen(int numbah){
//variables
boolean a = false;
int b = 2;
int c = 17;
//some pluggin' and chuggin'
if(numbah <= c)
if(numbah >= b)
a = true;
//return what you got (making sure that it is the correct return type)
return a;
}//end of function
So, there is a basic funtion, it should be fairly similar for other languages... I hope. anyway,
here is the only way that i can think to explain this.
where it says boolean in the line "public boolean....", this is the return type for the function. In this case, the function will return a boolean. You can return all sorts of things: int, double, string, char, etc., when the function is done running, you will return something of that type.
next comes the name of the function. The name of the function is to the right of the return type. here it is "twoSeventeen(...)." in order to call and use the function,
you will have to use this name. So, inside the parameter that will be accepted. Some functions don't have/need parameters, so you leave those blank. but in this case, the function accepts an integer and stores it in numbah. NOTE: any variables created inside of the function will die once the function has quit running.
next is the stuff it does (as in finding if the number provided is between 2 and 17), and doing crap. If the # is between those two, a is set to true, else nothing happens, and a remains false.
Lastly, you return something. if you don't, you'll have a major syntax error on your hands. this is done with the "return a;"
Finally you end it with a curly bracket/brace/whatever ya call it. NOTE: there has to be a bracket after the public......twoSeventeen(){, and at the end of the function.
That was about as bad as i could have put that, if you have any questions, just ask