Computer Science Canada Cant get Methods |
Author: | LOL123 [ Wed Apr 14, 2010 8:19 pm ] | ||
Post subject: | Cant get Methods | ||
Hi, I need to make a program that calculates the area of a circle. I have to get the input then use a method to calculate the area. I dont really understand methods I have read wtd's tutorial but still a bit unclear on how to create the method in my program and make it do what I need it to. This is my code so far:
|
Author: | TheGuardian001 [ Wed Apr 14, 2010 8:41 pm ] | ||||||
Post subject: | Re: Cant get Methods | ||||||
You know that
That you put in every program? That's a method. Creating a custom method works exactly the same way as writing main.
Where static and parameters are optional, and return type is any data type/class or void (no return.) You can name the method whatever you want (even if there's already a method with that name, so long as they have different parameters. Methods are generally public. If you want to call a method directly from main, it must be declared static (since main is always static). When you call a method, any code inside that method is run, after which the program will pick up where it left off. As an example, here's a method that will accept an integer, multiply it by 2, and return the new value.
|
Author: | slider203 [ Wed Apr 14, 2010 10:17 pm ] | ||
Post subject: | Re: Cant get Methods | ||
Also if you want your method stored in a diffrent file you can use code like this:
You will have to use an object to access the method in your program though. |