ASP.NET Basics (Part 6): Fully Function-al
(Page 1 of 8 )

Find out how to modularize your code in C# by abstracting it into functions. This segment of our ASP.NET tutorial covers defining and invoking functions, function return values and function arguments, together with a discussion of variable scope within an ASP.NET script.All the code you've seen over the past few weeks has been what might be called linear - written such that it is executed sequentially, one line after another. Sure, I've added a few "if" and "for" loops for variety, and even thrown in a "switch" here and there to break up the monotony - but by and large, the code in previous sections has been written to resemble a straight line.
Well, it's time to add a few swoops and swirls to the way you code, just to make sure you don't get bored (this also comes in handy if you need to confuse your boss). Enter this week's tutorial, which attempts to address the problem by teaching you all you need to know about a programming construct called a "function."
Ask a geek to define the term "function", and he'll probably tell you that a function is "a block of statements that can be grouped together as a named entity." Since this is a tutorial on ASP.NET, not the language Geek, I'll simplify that definition a little: a function is simply a set of program statements which perform a specific task, and which can be "called", or executed, from anywhere in your program.
Every programming language comes with its own built-in functions and typically also allows developers to define their own functions. For example, if I had a series of numbers, and I wanted to reduce each of them by 20%, I could pull out my calculator and do it manually, or I could write a simple C# function called cheatTheTaxman() and have it do the heavy lifting for me.
There are two important reasons why functions are a Good Thing. First, a user-defined function allows you to separate your code into easily identifiable subsections, thereby making it easier to understand and debug. And second, a function makes your program modular, allowing you to write a piece of code once and then re-use it multiple times within the same program.
Next: The Right Spirit >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire