Chapter 6: Functions


Overview

All the code you have seen so far has taken the form of a single block, perhaps with some looping to repeat lines of code and branching to execute statements conditionally. If you've needed to perform an operation on your data, then this has meant placing the code required right where you want it to work.

This kind of code structure is limited. You will often find that some tasks, for example finding the highest value in an array, may need to be performed at several points in a program. You can just place identical (or near identical) sections of code in your application whenever necessary, but this has its own problems. Changing even one minor detail concerning a common task (to correct a code error, for example) may require changes to multiple sections of code, which may be spread throughout the application. Missing one of these could have dramatic consequences and cause the whole application to fail. In addition, the application could get very lengthy.

The solution to this problem is to use functions. Functions in C# are a means of providing blocks of code that can be executed at any point in an application.

Note

Functions of the specific type examined in this chapter are known as methods. However, this term has a very specific meaning in .NET programming that will only become clear later in this book, so for now this term will not be used.

For example, you could have a function that calculates the maximum value in an array. You can use this function from any point in your code, and use the same lines of code in each case. Since you only need to supply this code once, any changes you make to it will affect this calculation wherever it is used. This function can be thought of as containing reusable code.

Functions also have the advantage of making your code more readable, as you can use them to group related code together. If you do this, then your application body itself can be made very short, as the inner workings of the code are separated out. This is similar to the way in which you can collapse regions of code together in VS using the outline view, and it gives a more logical structure to your application.

Functions can also be used to create multipurpose code, allowing them to perform the same operations on varying data. You can supply a function with information to work with in the form of parameters, and you can obtain results from functions in the form of return values. In the preceding example, you could supply an array to search as a parameter and obtain the maximum value in the array as a return value. This means that you can use the same function to work with a different array each time. the parameters and return value of a function collectively define the signature of a function.

In this chapter, you:

  • Look at how to define and use simple functions that don't accept or return any data.

  • Look at the way you can transfer data to and from functions.

  • Learn about the issue of variable scope. This concerns the way that data in a C# application is localized to specific regions of code, an issue that becomes especially important when you are separating your code into multiple functions.

  • Take an in-depth look at an important function in C# applications: Main(). You see how you can use the built-in behavior of this function to make use of command-line arguments, which enable you to transfer information into applications when you run them.

  • See an additional feature of the struct types that you saw in the last chapter, the fact that you can supply functions as members of struct types.

The chapter will end with two more advanced topics: function overloading and delegates.

  • Function overloading is a technique that allows you to provide multiple functions with the same name, but different signatures.

  • A delegate is a variable type that allows you to use functions indirectly. The same delegate can be used to call any function that matches a specific signature, giving you the ability to choose between several functions at runtime.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net