Function:
Data type -- variable constant -- operator Expression -- statement (order, branch, loop)-- array -- function
function in a program: a block of code that completes a relatively independent work.
Functions in Mathematics: Highly abstract.
Function four elements: function name, input, output, processing
function definition:
[Static] return type function name ( input parameter list )
{
function Body--processing
}
Function call:
Function name ( parameter ); --suitable for calling functions with no return value
The data type variable = function name ( parameter ); --suitable for calling functions that have return values.
Things to Master today:
1. what is a function?
2. can realize the benefits of the function: reusable, can division of labor, clear and easy to read
3. remember the four elements of the function.
4. A simple function is defined, and this function is called.
Example: function
Class Program//function more than add class, look for the analysis of each function do not find the wrong place
{
static void M1ain (string[] args)
{
Showheader ();
String city = Shuru ();
String Jieguo = Yunsuan (city);
Console.WriteLine ("Weather forecast only for reference:" + Jieguo);
}
private static string Yunsuan (String city)
{
Weather
String[] s = new string[] {"Fine", "fine", "clear", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "Sunny", "overcast", "cloudy", "Light Rain", "medium Rain "," Heavy Rain "," Rainstorm "," thunderstorm "," Snow "," Snow "," Snow "," Haze "," Fog "};
Random rand = new Random ();
int n = rand. Next (s.length); The subscript of the weather.
String TQ = S[n];
Temperature
int c = Rand. Next (70);
c-= 30;
String Jieguo = "Tomorrow's weather conditions:" + City + ", weather:" + TQ + ", Temperature:" + C;
return Jieguo;
}
private static string Shuru ()
{
Console.Write ("City:");
String city = Console.ReadLine ();
return city;
}
private static void Showheader ()
{
Console.WriteLine ("************* Weather Forecast ****************");
Console.WriteLine ("******** Zibo Meteorological Observatory Authority issued ****************");
Console.WriteLine ("******** If there is an anomaly, purely accidental ****************");
Console.WriteLine ("* * * * * * only for personal testing and hobby use, not for commercial operation **********");
}
}
Example: Green Song race score
Class Class1
{
static void Main (string[] args)
{
Int[] A = new int[10];
Scoreboard
Shuru (a);
Sort
Paixu (a);
Arithmetic averaging
Double avg = Yunsuan (a);
Output display
Shuchu (A, AVG);
}
private static void Shuchu (int[] A, double avg)
{
Console.WriteLine ("Remove two highest points:" + a[0] + "and" + a[1]);
Console.WriteLine ("Minus two minimum points:" + a[a.length-1] + "and" + a[a.length-2]);
Console.WriteLine ("The contestant eventually has to be divided into:" + avg);
}
private static Double Yunsuan (int[] a)
{
To find the total
int sum = 0;
for (int i = 2; I <= a.length-3; i++)
{
Sum + = A[i];
}
Find average
Double avg = (1.0 * sum)/(A.LENGTH-4);
return avg;
}
private static void Paixu (int[] a)
{
for (int i = 1; I <= a.length-1; i++)
{
for (int j = 1; J <= A.length-i; j + +)
{
if (A[j] > A[j-1])
{
int temp = A[j];
A[J] = a[j-1];
A[J-1] = temp;
}
}
}
}
private static void Shuru (int[] a)
{
for (int i = 0; i < a.length; i++)
{
Console.Write ("Please" + (i + 1) + "Judge Scoreboard:");
A[i] = Convert.ToInt32 (Console.ReadLine ());
}
}
}
C # 7 function Green song scoring weather forecast