Powered by Blogger.

Sunday, February 23, 2014

Functions and Subroutines in C#.Net

Functions and subroutines (subs) are very similar; both enable you to create a reusable block of code that you can call from other locations in your application. The difference between a function and a subroutine is that a function can return data whereas a sub doesn’t. Together, functions and subroutines are referred to as methods.

Also check out:

- How to display serial number automatically in GridView in asp.net?

- Use of Extern Alias in C#.Net

- Caching in Asp.Net

They can be parameterized. That is, you can pass in additional information that can be used inside the function or subs.

‘ Define a function
Public Function FunctionName ([parameterList]) As DataType
End Function

‘ Define a subroutine
Public Sub SubName ([parameterList])
End Sub

// Define a function
public datatype FunctionName([parameterList])

// Define a subroutine
public void SubName([parameterList])

The complete first line, starting with Public, is referred to as the method signature because it defines the look of the function, including its name and its parameters. The Public keyword (public in C#) is called an access modifier and defines to what extent other web pages or code files can see this method.

The name of the function is followed by parentheses, which in turn can contain an optional parameter list. Both the function and subroutine have a parameter list that enables you to define the name and data type of variables that are passed to the method. Inside the method you can access these variables as you would normal variables.