C # Basic functions

Source: Internet
Author: User

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 function of a subroutine is composed of a main function and a number of functions. Other functions are called by the main function, others

Functions can also be called to 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 placed in the library for public

Selection. Be good at using functions to reduce the amount of repetitive programming segments.

Void is not a return value, in parentheses is a parameter
Private only in the current class, public, can be used throughout the namespace

function type

The first type: no reference, no return
public void Leijia ()
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}

The second kind, there is no return
public void Leijia (int a)
{
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
Console.WriteLine (sum);
Console.ReadLine ();
}


The third kind, has the participation to return
public int Leijia1 (int b)
{
int sum = 0;
for (int i = 1; I <= b; i++)
{
sum + = i;
}
return sum;
}


The fourth kind, has the return without the reference
public int Leijia2 ()
{
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
sum + = i;
}
return sum;
}

C # Basic 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.