Chapter 6

Functions

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to define and use simple functions that don’t accept or return any data
  • How to transfer data to and from functions
  • How to work with variable scope
  • How to use command-line arguments with the Main() function
  • How to supply functions as members of struct types
  • How to use function overloading
  • How to use delegates

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

You can find the wrox.com code downloads for this chapter at www.wrox.com/remtitle.cgi?isbn=9781118314418 on the Download Code tab. The code is in the Chapter 6 download and individually named according to the names throughout the chapter.

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. Performing an operation on your data has meant placing the code required right where you want it to work.

This kind of code structure is limited. Often, some tasks—such as finding the highest value in an array, for example—might need to be performed at several points in a program. You can place identical (or nearly 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) can require changes to multiple sections of code, which can be spread throughout the application. Missing one of these can have dramatic consequences and cause the ...

Get Beginning Visual C# 2012 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.