Understanding the Basics of Methods
In this article you will learn the basics of methods including what it is, what it is used for, why you should use them, and the different types of methods.
Why is this important to learn about?
Methods are important in keeping your code clean, organized and un-repetitive. Methods also simplify the program so others can easly read it.
Definitions of terms used.
- A Method preforms the action in its statement block
- A statement block is the code inside of the curley Q brackets.
Example
static void Main()
{
//statement block
}
Note: All examples were created using Visual Studio 2010 Pro, targeting the .NET Framework 4.0. We'll do our best to point out anything that might not work in older versions.
Understanding The basics of Methods
Lets first start out with a snippet of code you see everytime you start a Console Applacation project.
static void Main()
{
}
There are three discussion points here.
The first is, “what the heck is static void”?
- Static means that the method (or property/constructor/field/etc...) does not belong to an instance of the class, but is shared across all instances, and is to be accessed through the class name, rather than an instance. (Thank for the better definition)
- void tells the program that you do not want to return anything.
Next, you see the identifier “Main”.
When the program runs, this method will be called first because it is called Main. You can name methods just about anything, except for keywords, but it is important to name the method something related what its purpose is.
Lastly, is the parenthesis after Main.
Those parenthesis hold the arguments that you want you pass in or out of the Method.
Here is an example:
Method Type 1
static void Main()
{
int first = 2;
int last = 3;
int answer = 0;
//calling our addition method
addition(first, last, ref answer)
Console.WriteLine(answer);
Console.ReadLine();
}
static void addition(int first, int last, ref int answer)
{
answer = first + last;
}
We first initialize our variables, then we call our method. When calling a method, you need to include in the parenthesis the names of the variables you what passed to the method. You may be asking yourself, “but what about that ref thing before answer??”. Well, that tells the compiler that you want the value of answer to be returned to Main.
If you enjoy the technical reason, when you do not include ref in front of the argument the compiler assumes that you what it passed in by value, but when you include ref, you are telling the compiler that you want to reference the memory address to the method to be modified.
Now, in our method we need to declare the variables we need to use. The format is, “data type” “variable name”. If you have more than one variable, like we do, you separate each one with a comma. You can name the variables virtually anything, it doesn’t need to be the same is the variables in Main. The important thing is that you keep the same order.
I can name first, one; last, two; and answer, result, as long as it is in the same order as the call like so:
Static void addition(int one, int two, ref int result)
{
result = one + two;
}
You need to initialize your variables before you pass them to the method. As you can see I set answer to 0, but once the method returns the correct answer it will overwrite that. We will get to a way around having to initialize variables later.
Another way to do the same program would be:
Method Type 2
static void Main()
{
int first =2;
int last = 3;
int answer = addition(first, last);
Console.WriteLine(answer);
Console.ReadLine();
}
static int addition(int first, int last)
{
int answer = first + last;
return answer;
}
Instead of referencing the variable I what returned to Main, I can change, “static void” to “static int” which tells the compiler that I want you to return something using the data type integer.
The program could also look like this:
static void Main()
{
int first =2;
int last = 3;
int result = addition(first, last); //look here
Console.WriteLine(result);
Console.ReadLine();
}
static int addition(int first, int last)
{
int answer = first + last;
return answer;
}
I change answer to result to further illustrate that variable names do not need to be the same when passing in and out of a method.
Just like a 2 AM infomercial, there is one more amazing deal in this offer!
Method Type 3
static void Main()
{
int first =2;
int last = 3;
int answer; //look here
//calling our addition method
addition(first, last, out answer);
Console.WriteLine(answer);
Console.ReadLine();
}
static void addition(int first, int last, out int answer) //and here
{
answer = first + last;
}
Here I am using what is called an out. And out acts just like a ref, but I do not need to initialize answer.
In Conclusion
In conclusion, methods are a very important part of coding and should be used often. You do not need to name a variable inside a method the same as the variable you are passing into the method. There are three different was to return a value from a method, that is by reference, out, or return.
This is my first, or second depending on which is approved first, tutorial, so if you have any suggestions on how I can improve future tutotials please let me know.
Thanks,
Richey
See all the C# Learning Series tutorials here!





MultiQuote




|