Text Size

FUNCTIONS IN C#

PDF 

Tutorials

(3 votes, average: 5.00 out of 5)

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.

Trackback(0)
Comments (2)add comment

van said:

0
...
how do you know if your input is wrong??
 
September 02, 2008
Votes: +0

sampath said:

0
...
how do i start learning c#.net,asp.net,ado.net
 
December 08, 2008
Votes: +1

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1