Declaring Functions


A function is a series of statements that can be invoked more than once. As you're writing your program, you may discover that certain lines of code are being repeated throughout the program (for example, the code needed to open a database connection). It makes sense to put that code into a function.

Functions have input variables known as parameters and they also have an output value known as the result. Adding parameters and a return to a function will be discussed in the next few topics. The simplest type of function is one that doesn't have any input parameters and doesn't have a result value.

To declare a function that takes no input and doesn't return a value:

  1. Type void followed by a space.

  2. Type the name of the function.

  3. Type an open parenthesis, followed by a close parenthesis () .

  4. Type an open curly bracket { .

  5. Enter the code for the function.

  6. Type a close curly bracket } ( Figure 2.29 ).

    Figure 2.29 The ClearFields function doesn't depend on any input to carry out its task. It also doesn't return any values to the caller.
     void ClearFields() {    txtItem.Text = "";    txtDescription.Text = ""; } 

graphics/tick.gif Tips

  • In the old days of programming (before .NET, to be more accurate) developers would often declare their functions with a return parameter of int, rather than void, to return a success or failure code. Some developers would return zero if the function succeeded and a non-zero number if the function failed. Other developers would return a zero if the function failed and a non-zero number if the function succeeded. And that was the problem with error codes: No one ever knew whether a zero was a good thing to get or not. For that reason and others Microsoft decided to use exceptions (a type of class) to report error conditions in the .NET Framework and they recommend that you do so also.

  • Functions must be declared inside classes. Other languages like C++ support stand-alone functions (functions that aren't part of a class), but C# doesn't ( Figure 2.30 ).

    Figure 2.30 Languages like C and C++ support standalone functions, but C# doesn''t. In C# functions need to be part of a type like a class. The only code outside of a class is code that defines a type, like enum declarations.
     //it is illegal to have stand-alone functions in C#. //Functions need to be declared inside of a class. void DoTask1() //this is illegal { } class TruthTeller {  //this is legal   bool WillIGraduate()   {   return false;   }  } 
  • There are two main types of functions: instance functions and static functions. The above examples all include instance functions. These are functions that require you to create an instance of a class before using them. Static functions don't require you to have an instance of a class. You call static functions by using the name of the class plus the name of the function. Figures 2.31 and 2.32 show you the difference between the two. Chapter 6, "Special Members," talks about how to declare and use static functions.

    Figure 2.31 You invoke static methods using the class name-dot-function. To invoke instance method you first create a new instance of a class, assign it to a variable, then use variable-dot-function.
     class Account {  public void PrintBalance()  {    } } class TruthTeller {  public static bool AmIOld(byte Age)  {      if (age > 40)         return true;      else         return false;    } } class App {    static void Main()    {       //to use an instance method you       //have to declare a variable of the       //type of class the method is in       //and set it equal to a new       //instance of the variable  Account acct = new Account();   acct.PrintBalance();  //with static methods you simply       //use the name of the class, a dot,       //and the name of the method.  bool Old = TruthTeller.AmIOld(45);  //this would be illegal:       //can't call static method       //through instance variable       TruthTeller Truth =       new TruthTeller();       bool Old = Truth.AmIOld(45);    } } 
    Figure 2.32. With instance fields and instance functions, it matters what variable you use to call the function. With static methods, it doesn't, you always use the class name to invoke it. Static functions aren't dependent on any one instance of the class.

    graphics/02fig32.gif




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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