Returning Function Values


In the previous section you saw that some parameters could be used to return data from a function. These parameters were ref and out parameters. In addition to using parameters, each function can also return a value as the outcome of the function. This special value is known as the return value. When you declare the function you can specify the type of value you wish to return. As you saw earlier, functions that don't return a value are declared with the void keyword.

To return a value from a function:

  1. Instead of the word void , type the name of the type you wish to return before the function name.

  2. Inside the body of the function, type return followed by a space and the value you wish to return.

  3. Type semicolon to end the statement ( Figure 2.39 ).

    Figure 2.39 Functions can also have a return value. Use return values when the function clearly has a single form of output. If you need to return more than one result, you should use out parameters.
     class TruthTeller {  bool  WillIEverBeRich(string profession)    {      if (profession == "Writer")  return false  ;      else  return true;  } } 

graphics/tick.gif Tips

  • You can return a value using a literal or a variable ( Figure 2.40 ).

    Figure 2.40 It is fine to return a literal or a value stored in a variable as the result of the function.
     class Checking {  string  GetAccountType()    {  return "Checking";  //returning a                          //literal value    }  int  MakeDeposit(int amt1, int amt2)    {      int total = amt1 + amt2;  return total;  //returning a                    //variable    } } 
  • When you use return , not only do you set the output parameter of the function, but you also exit the function immediately. If there is code after the return, it won't be executed.

  • The C# compiler gives you an error if your function was declared to return a value but you forgot to use return ( Figure 2.41 ).

    Figure 2.41 If you put a return type in the function declaration, it becomes mandatory to return a value.
     class Mother {    bool ApprovesOfGirlfriend(string name)    {  //error, Mother can't be silent, she   //must return true or false  }    int AskForMoney(int Amount)    {      if (Amount > 20)         return 20;  //error, what if amount is less or   //equal to 20, there is no return   //for that case. All cases must return   //something.  } } 
  • Functions that use void for a return type can still use return without a value next to it ( Figure 2.42 ).

    Figure 2.42 return can be used even when the function doesn't have a return type to exit the function prematurely.
     class Savings {    bool accountClosed;    void CloseAccount(int accountNum)    {      if (accountClosed == true)  return;  //return here is used to                 //exit function                 //prematurely, in this                 //case because the                 //account is already                 //closed.      //...close the account      //mark status as closed      accountClosed=true;    } } 



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