C # Part---function add basic format;

Source: Internet
Author: User

Format 1: No parameters, no return value (no parameter returned)

To add a function:

/// <summary>///summation method, no parameters, no return value/// </summary> Public voidLeijia () {//Accumulate sumConsole.Write ("Please enter a positive integer:");intA =int. Parse (Console.ReadLine ());intsum =0; for(inti =1; I <= A; i++) {sum+=i;} Console.WriteLine ("sum is"+sum);}

(The main function of the wording:)

Static void Main (string[] args)    {// instantiation initializes the class new program (); // Hanshu. First (); // Format 1 Call // Hanshu. Leijia (); }

Format 2: No return value, parameter (with reference not returned)

To add a function:

/// <summary>///Add sum, require an int type parameter, no return value/// </summary>/// <param name= "D" ></param> Public voidLeijia (intd) {intsum =0; for(inti =1; I <= D; i++) {sum+=i;} Console.WriteLine ("sum is"+sum);}

(The main function is written in the notation):

Static void Main (string[] args)        {            // instantiation   Initializes   this class            new Program ();             // Format 2 Call             // Console.Write ("Please enter a positive integer:");             // int a = Int. Parse (Console.ReadLine ());             // Hanshu. Leijia (a);       }


Format 3: Parameters, with return value (with reference to return)

To add a function:

/// <summary>///Add sum, need parameter, return value/// </summary>/// <param name= "D" ></param>/// <returns></returns> Public intLeijia1 (intd) {intsum =0; for(inti =1; I <= D; i++) {sum+=i;}returnsum;}

(notation in the main function:)

 static  void  Main (string  [] args) { // instantiation initialization of this class  program Hanshu = new              program ();  //  format 3 call  // console.write (" Please enter a positive integer: ");  // int a = Int.            Parse (Console.ReadLine ());  // int sum = Hanshu.            Leijia1 (a);        // console.writeline (sum);  }

Format 4: No parameters, return value

To add a function:

/// <summary>///Add sum, no arguments, but with return value (int)/// </summary>/// <returns></returns> Public intLeijia2 () {Console.Write ("Please enter a positive integer:");intA =int. Parse (Console.ReadLine ());intsum =0; for(inti =1; I <= a;i++) {sum+=i;}returnsum;}

(notation in the main function:)

Static void Main (string[] args)        {            // instantiation   Initializes   this class            new Program ();            ;             // Format 4 Call             // int sum =hanshu. Leijia2 ();             // Console.WriteLine (sum);          }

When there are two or more variables in format 3, there are parameters to return such as comparison size

Two number comparison size returns the larger one

To add a function:

 /// </SUMMARY>  /// <param name=" a "></PARAM>  /// <param name=" B "></PARAM>  /// <RETURNS></RETURNS>  public  double  Max (double  A, double   b) { if  (A >= b) { return   A;}  else  { return   b;}  

(notation in the main function:)

 static  void  Main (string  [] args) { // instantiation initialization of this class  program Hanshu = new              program ();  //  compare size  double  a = 6  ;  double  b = 7             ;  double  c = 4             ; Console.WriteLine (Hanshu. Max (Hanshu.         Max (A, B), c)); }

C # Part---function add basic format;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.