Tutorials
Desktop Programming
C Sharp
FUNCTIONS IN C#
Written by Christopher Thursday, 29 May 2008 13:28
Tutorials
INTRODUCTION
Functions are a must for modern object oriented programming and for visual programs as well. When you click a button on your mouse an event is generated. This event fires a function that is linked to that particular event. Therefore, you need to define several functions that correspond to various actions your code should perform. They are also useful when you need to call a block of code many times in different parts of your program.
FUNCTIONS VS SUBROUTINES
An important thing to note is that C#, in contrast to Visual Basic, does not distinguishes between functions and subroutines. C# combines these two functionalities in one, namely a method, or a function. Hereafter the terms method and function will be used interchangeably. The difference between a subroutine and a function is that a function returns something (an integer for example) while a subroutine does not. Methods in C# can either return a value or not. The value that a method will return is specified by the programmer during the declaration of the method.
THE MAIN() FUNCTION
Each program in C# must have a function called main(). This function can return either nothing or an integer. In case you do not want the function to return anything you specify its return type as void. The main() function is the entry point for every program. When a program starts executing it starts from this function.
SYNTAX AND USAGE OF C# FUNCTIONS
The general format for defining a method in C# is:
[modifiers] return_type methodName([parameters])
The modifiers are optional keywords that define where the method can be called from. Two modifiers are static and public. If a method is defined as public, it can be called from anywhere, inside or outside its class. The static modifier means that the method is connected to the class as a general/abstract concept and not to its instances. Therefore, it can be called without first instantiating the class to an object. Finally, to exit a method you call the return statement. An example of a main() method is depicted below. This method prints the “Hello world” statement in the console and then exits, returning an integer with value 4.
static int Main(string[] args)
{
Console.WriteLine("Hello World");
return 4;
}
VARIABLES AND FUNCTIONS
Variables that are defined inside a function go out of scope when the function ends. Suppose we have the following method:
public int function1(string[] args)
{
int i = -4;
Console.WriteLine("Hello World");
return i;
}
Variable i is declared and initialized inside the function. So, when the function terminates this variable will go out of scope. This is always true for data type values. For reference types, they might not go out of scope when the function ends. This can happen for example, if we also assign the referenced object to another visible variable that is declared outside and before this function, having a global scope. In this way, when the function terminates, the global variable will still be in scope and reference the object created inside the function. So, this object is still referenced to something and cannot be destroyed by the Garbage collector.
Another important thing is that you can pass either value types or reference types in a function. If you pass a value type object, a copy of the object will be created when entering the function. If you pass a reference type, no copies will be created and the reference of the actual object is passed. In this case any changes to the object passed will affect the original as well.

| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |