Pages

Sunday, 14 June 2015

About Methods

Method

A method is a block of code with a name. You can execute the code from somewhere else in the program by using the method’s name. You can also pass data into a method and receive data back as output. As you saw in the previous section, a method is a function member of a class. Methods have two major sections, as shown below:
  • Header specifies the method’s characteristics, including the following:
    • Whether the method returns data and, if so, what type
    • The name of the method
    • What types of data can be passed to and from the method and how that data should be treated.
  • Body contains the sequence of executable code statements. Execution starts at the first statement in the method body and continues sequentially through the method.
Here's the simple syntax of method:
[Access Modifier : Optional] [Return Type] [Method Name] ([Parameter List])
{
 [Method Body]
}
Following code is method example, converting seconds to hour format.
void showTime(uint secs)
{
 uint hours, minutes, seconds;
 hours = secs / 3600;
 secs %= 3600;
 minutes = secs / 60;
 seconds = secs % 60;
 Console.WriteLine("it's: " + hours + "Hr(s): " + minutes + "min(s) : " + seconds +"sec(s)");
}

line 1 Method's Head, containing return type void, method's name showTime and secs as parameter.
line 3-8 Method's body containing 6 statements.
line 3 declaration of local variables hours,minutes and seconds
line 4-7 Operation to convert seconds to hour format
line 8 Display to console conversion result

Return Values

A method can return a value to the calling code. The returned value is inserted into the calling code at the position in the expression where the invocation occurred.
  • To return a value, the method must declare a return type before the method name.
  • If a method doesn’t return a value, it must declare a return type of void.
The following code shows two method declarations. The first returns a value of type uint
uint convertHours(uint hours, uint minutes, uint seconds)
{
 return hours * 3600 + minutes * 60 + seconds;
}
You can also return a value from userdefined types, as follow code
static DateTime GetTime()
{
 DateTime now = DateTime.Now;    // Get the current date and time.
 return now;
} 

The Return Statement and Void Methods

In programmer's world, sometimes we need to simplify the program logic by exiting the method early when certain conditions apply. To exit the voidmethod, use return statement without any value following.

Let's modify our showTime function, when supplied parameter seconds is 0 then do nothing and exit the method.
static void showTime(uint secs)
{
 if (secs == 0)   //when supplied secs parameter is 0, exit the method
  return;
 uint hours, minutes, seconds;
 hours = secs / 3600;
 secs %= 3600;
 minutes = secs / 60;
 seconds = secs % 60;
 Console.WriteLine("it's: " + hours + "Hr(s): " + minutes + "min(s) : " + seconds +"sec(s)");
}

Method Invocation

You can can call other methods from inside a method body. These are the rule to call a method:
  • The phrases call a method and invoke a method are synonymous.
  • You call a method by using its name, along with the parameter list
static void Main(string[] args)
{
 uint seconds = 7546;
 Console.Write("convert " + seconds + " seconds => ");
 showTime(seconds);              //invoke showTime

 uint hours = 1, minutes = 20;
 uint convertedSeconds;
 seconds = 30;
 convertedSeconds = convertHours(hours, minutes, seconds);           //invoke convertHours and store return value to convertedSeconds
 Console.WriteLine(hours + "hr(s):" + minutes + "min(s):" + seconds + "sec(s) is " + convertedSeconds +" seconds");

 DateTime now = GetTime();           //invoke GetTime and store return value to now
 Console.WriteLine("Now is " + now.Hour + ":" + now.Minute + ":" + now.Second);
 Console.Read();
}
The code will result:
convert 7546 seconds => it's: 2Hr(s): 5min(s) : 46sec(s)
1hr(s):20min(s):30sec(s) is 4830 seconds
Now is 16:17:37

0 comments:

Post a Comment