Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Working With Functions(A.K.A Methods)

variable type

  • Please log in to reply
2 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 09 February 2011 - 07:24 PM

Ello, everyone! :)

Alright lets jump right in and go over how this is gonna work. In this tutorial we will go over Functions! I will explain what we are doing, or what need to do, and try to comment out every line of code, so you know exactly whats happening in every line.

Be for we get started, create a new windows form application, and just add a button along with a buttonClick event.

So lets explain what a function is, and how its different from a method.

What Is a Function? How Is It Different From A Method?
To answer the first question, a function is a block of code which we can call upon from multiple places in our code. This is very useful if you need to repeat a piece of code, but dont want to write it out 2 or 3 times. Now the difference between a method and a function as I see it is; A method is a function declared within a class. So if you create a "function" in your code, its called a function, but if you create a function in a separate class, its called a "Method". (Hope that was understandable)

How A Function Works, And Where To Use It.
First lets go over where to use a function. You should use a function anywhere you would normally have to write the same code over multiple times. For example if you wanted to write a big line of text into a textBox multiple times, it would be wise to create a function that did this for you. Then you wouldnt have to write the big line of text every time you needed it to go into the textbox, you would just let the function do it for you.
Another reason to use a function is to clean up code. If for instance you want to write the big line of text to the textBox and do some other things, it would be nice to just call the function and let it do the work for you while keeping the mess elsewhere, making it much easier for you to work with the "other things".

Now hopefully you where able to understand what I was saying above. Lets learn how a function works!
We declare a function like this:
<Access Modifier> <return type> <name> (<parameters>)
{
<OurCodeGoesHere>
}

What do these things mean?
<modifier> - View Link
This link will show the different type's of modifiers and what they do.
Access Modifiers (C# Programming Guide)

<return type> - This can be any variable type: Int, Double, String, Char ect...
This is the type of return variable we want to use in our function. When we create a function we can create a "return" statement that will return the value of the given variable, so we must assign the type of variable we want to return. If you dont have any variable value to return you simple type "void" meaning its void of a return value.

<name>
Simply what ever you wish to name function, but also how we will reffer to it. Since we refer to the function by its name its normally named something descriptive to what it does.

<parameters>
- Any variable type or object.
Parameters allow us to take dynamic value's and use them in our function. For instance if we wanted to create a function that took the users name and display it in our function, we would have a string parameter. As we called that function we could fill that parameter with the username and the function could display it for us. If you dont want any parameters, you can simply leave the parenthesis empty.
We'll have some examples later.

<OurCodeGoesHere>
Pretty obvious, but this is where the code we want to execute will go in the function.

Alright you should understand the basics of a function, so lets continue.

Creating Our First Function.
Lets go ahead and create our first function! In this function we will simply display a welcome message in a messageBox.
public void DisplayWelcome()[COLOR=green]//Create a new public function with no return type, named "DisplayWelcome".[/COLOR]
{
MessageBox.Show("Welcome User");[COLOR=green]//Welcome the user...[/COLOR] [COLOR=red]lol[/COLOR]
}
Now that we have a function declared we can call it, in our buttonClick event like so:
DisplayWelcome();[COLOR=green]//Call the function by its name "DisplayWelcome" follow by parentheses '(' and ')' which is where parameters would go if we had any.(we'll go over them shortly)[/COLOR]
And thats really the basics of a function. I hope your starting to understand how they work.

Using "return" In Our Function.
Now, lets learn to make our function a little more dynamic, by using the "return" statement.

As we went over before we can return a variable from are function with the "return" statement. So lets create a function that returns someones userName.
public string  DisplayWelcome()[COLOR=green]//Create our public function, this time with a return type of "string".[/COLOR]
{
string userName = "John";[COLOR=green]//Create a new userName string, John.[/COLOR]
return userName; /[COLOR=green]/return the value of userName( or "John" basically)[/COLOR]
}
Now with the above function declared we can call it in our code, although we call it slightly different from our first example, since its actually returning the value of userName we can use it, in a messageBox like this:
MessageBox.Show("Welcome, " + DisplayWelcome().ToString());
Then when the function is executed it will display "Hello, " plus the return value of "DisplayWelcome" which is userName, giving the outcome of, "Welcome, John".

Using Parameters.
We will once again use our "DisplayWelcome" function, although it will be a little different since where adding parameters. As you know, parameters will allow us to take value's given when we call the function. For example, this time lets actually assign the userName when we call the function, so its not just set as "John".
public void DisplayWelcome(string Name)[COLOR=green]//Our previous function, now with a string parameter(Name) to hold the value of our name.(notice a void return type since we wont be needing any return value's.)[/COLOR]
{
MessageBox.Show("Welcome, " + Name);[COLOR=green]//This will display "Welcome, " then the value of Name, which we give when calling the function.[/COLOR]
}
Now to call out function in the buttonClick event
DisplayWelcome("c0der");[COLOR=green]//We call our "DisplayWelcome" filling the parameter with the string "c0der".[/COLOR]
Run your application and you should see "Welcome, c0der" or whatever name you entered for the parameter.

And thats it for functions!

Wrapping Up.
So we've learn what a function is, how its different from a Method, and where to use them in our code. We also learn how to use functions, the "return" statement, and parameters. I hope that covers everything, and that you enjoyed this tutorial without to much confusion.

As always +rep, comments, and suggestions welcome!
Happy C0ding ~ Committed. :)
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 14 February 2011 - 09:45 PM

Nice work mate,

The good thing about this tutorial is that it can be used as an example for multiple languages other than C#
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 14 February 2011 - 10:37 PM

Thank you for the positive feed back. :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.





Also tagged with one or more of these keywords: variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download