C # language Basics-Functions

Source: Internet
Author: User
Tags types of functions

Function
A larger program should generally be divided into a number of blocks, each of which is used to implement a specific function. All high-level languages have the concept of subroutines, which implements the function of modules with subroutines. In the C # language, the role of a subroutine is composed of a main function and a number of functions. Other functions are called by the main function, and other functions can be called each other. The same function can be called any number of times by one or more functions.
In the program design, some common function modules are often written into functions, which are put in the library for public selection. Be good at using functions to reduce the amount of repetitive programming segments.
Function: A module capable of accomplishing a function independently.
Function four elements: input, output, function body, functional name
function definition:
(static/public) return type function name (parameter type parameter name, parameter type argument name)
{
function body
}
Call to function:
Return variable type variable name = function (argument value)
II. Case Specific Analysis function type
Using the function method to solve the factorial and the 1!+2! +3! +...+n! --four types of functions
namespace function
{
Class Program
{
<summary>
First type: No return, no reference
</summary>
public void Jiehe ()
{
Console.Write ("Enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
<summary>
The second type: no Return and no reference
</summary>
<param name= "a" ></param>
public void Jiehe (int a)
{
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
Console.WriteLine (sum);
Console.ReadLine ();
}
<summary>
The Third kind: Has the return to have the argument
</summary>
<param name= "a" ></param>
<returns></returns>
public int jiehe1 (int a)
{
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
return sum;
}
<summary>
The fourth type: return without a reference
</summary>
<returns></returns>
public int jiehe2 ()
{
Console.Write ("Enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
return sum;
}
static void Main (string[] args)
{
Corresponding to the first to fourth function above, the main function in main
The first type: no return, no parameter:
Program Hanshu = new program ();
Hanshu.jiehe ();
The second kind: no return to have the parameter:
Console.Write ("Enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
Program Hanshu = new program ();
Hanshu.jiehe (a);
The Third kind: Has the return to have the parameter:
Console.Write ("Enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
Program Hanshu = new program ();
int sum = HANSHU.JIEHE1 (a);
Console.WriteLine (sum);
Console.ReadLine ();
The fourth type: return without reference:
Program Hanshu = new program ();
int sum = Hanshu.jiehe2 ();
Console.WriteLine (sum);
Console.ReadLine ();
}
}
}
Third, examples
1, Namespace Hanshu
{
Class Program
{
<summary>
Enter a two-digit comparison size to return a larger
</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;
}
}
static void Main (string[] args)
{
Write a function that can compare the size
Three number comparison size
Double A = 3;
Double b = 5, c = 2;
Program Hanshu = new program ();
Double max = Hanshu. Max (Hanshu. Max (A, B), c);
Console.WriteLine (max);
Console.ReadLine ();
}
}
}
2, use the method of the function to make rabbits rabbit

C # language Basics-Functions

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.