Page 1 of 1

Understanding the Basics of Methods

#1 Gavisann  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 382
  • Joined: 01-July 11

Posted 08 July 2011 - 01:37 PM

*
POPULAR

Learning C# Series
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!

Is This A Good Question/Topic? 5
  • +

Replies To: Understanding the Basics of Methods

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,199
  • Joined: 02-June 10

Posted 09 July 2011 - 08:11 AM

Just a suggestion... Enter your code into a project in Visual Studio and clean up all the indenting (CNTRL-K, CNTRL-D). Then copy/paste the code into your posts. This will keep all the indenting and give a neater look that more closely matches what your readers will see in VS.

It also helps you find bugs.

For example:
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;
}




versus
static void Main()
{
int first = 2;
int last = 3;
int answer = 0;

//calling our addition method
addition(first, last, ref answer) // <---------- Missing semi-colon
Console.WriteLine(answer);
Console.ReadLine();
}

static void addition(int first, int last, ref int answer)
{
answer = first + last;
}



While a missing semi-colon is a trivial typo, to anyone so new as to be in need of the tutorial it will keep the example from working and might take hours for a newbie to reason out *why*

This post has been edited by tlhIn`toq: 09 July 2011 - 08:12 AM

Was This Post Helpful? 3
  • +
  • -

#3 Gavisann  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 382
  • Joined: 01-July 11

Posted 09 July 2011 - 08:35 AM

Haha, thank you!

I did copy and paste some of the code, but that one I didn't because it was a simple statement.... Next time I will debug all my of code, thank you for finding that.

Also it should say, "Thank Curtis Rutland for the better definition" and not "Thank for the better definition."

This post has been edited by Gavisann: 09 July 2011 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 chancomp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 12

Posted 29 November 2012 - 07:18 PM

Great Job Gavisann. I am learning C# and your 3 methods to do the same thing answered a lot of questions for me. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1