Functions in C#


A method is a group of statements that work together. Every C # program has at least one class whose name is the main name
To use a method, you need to −
  • Define the method
  • Call the method

Defining Methods in C#

When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows −
<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}
Following are the various elements of a method −
  • Access Specifier − This determines the visibility of a variable or a method from another class.
  • Return type − One method can return a value, the type of return is the method that returns the method. If the method is not returning any value then the return type is zero.
  • Method name − The law name is a unique identifier and this case is sensitive. It can not be similar to any other identifier declared in the class.
  • Parameter list − Connected between parentheses, the criteria are used to obtain and obtain data from any method. Parameter refers to the type, order and number of parameters of any method in the list. Parameters are optional; That is, one method can not have any parameters.
  • Method body − This contains the set of instructions needed to complete the required activity.

Example - 

class NumberManipulator {

   public int FindMax(int num1, int num2) {
      /* local variable declaration */
      int result;

      if (num1 > num2)
         result = num1;
      else
         result = num2;

      return result;
   }
   ...
}

Calling Methods in C#

You can call a method using the name of the method. The following example illustrates this −

Example - 

using System;

namespace CalculatorApplication {

   class NumberManipulator {
   
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;
         
         if (num1 > num2)
            result = num1;
         else
            result = num2;
         return result;
      }
      
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

Recursive Method Call

A method can call itself. This is known as recursion. Following is an example that calculates factorial for a given number using a recursive function −

Example - 

using System;

namespace CalculatorApplication {

   class NumberManipulator {
   
      public int factorial(int num) {
         /* local variable declaration */
         int result;
         if (num == 1) {
            return 1;
         }
         else {
            result = factorial(num - 1) * num;
            return result;
         }
      }
      
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         //calling the factorial method {0}", n.factorial(6));
         Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
         Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
         Console.ReadLine();
      }
   }
}

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment