Function
C # is to implement modular programming through static functions.
1.1 Definition of function:
Any function is composed of the function description and the function body two parts.
Depending on whether the function requires parameters, the function can be divided into:no parameter functionAndparameter function。
1, the general form of the non-parametric function:
function type function name (function parameter)
{
Description statement part;
Executable statement part;
}
2, the general form of the parameter function:
function type function name (data type parameter [, data type parameter 2 ...] )
{
Description statement part;
Executable statement part;
}
there is a parameter table with the parameter function more than the parameterless function. When you call an argument function, the calling function assigns the actual values to those parameters.
In order to distinguish it from the actual parameters provided by the calling function, the parameter table in the function definition is called the formal parameter table, or the formal parameters list.
The case defines a function that is used to calculate a large number in two numbers.
/* Function: Define a larger number of functions and call in the main function */
static int Max (int n1,int n2)
{
return (N1>N2?N1:N2);
}
static void Main (string[] args)
{
int num1,num2;//declaration of two variables
num=4;//Assignment Value
num2=7;//Assignment Value
Call the Max function
int S=max (NUM1,NUM2);
Console.WriteLine (s);
Console.ReadLine ();
}
1.2. function return value and function type:
Functions can be divided into a return function and no return function of the two.
1, function return value and return statement:
The return value of the parameter is obtained through the return statement in the function.
Note: There is no return statement in the calling function, not a value is returned, but an indeterminate value. To make it clear that no return value is returned, you can define "void" as a "no (empty) type."
2. Function Type:
When defining a function, the description of the function type should be the same as the type of the return statement in which the expression is returned.
If they are not consistent, the function type will prevail. If the default function type, the system is treated as integers.
Good programming habits: in order to make the program good readability and reduce errors, any function that does not require a return value should be defined as an empty type, even if the function type is an integer and the default handling of the system is not used.
1.3, the function of the call:
In a program, the function body is executed by a call to the function, and the procedure is similar to the subroutine invocation in other languages.
The general form of a function call is: the function name ([actual parameter table]);
Remember that the number, type, and order of arguments should be consistent with the number, type, and order of parameters required by the called function in order for the data to be passed correctly.
Several function call methods:
1, function expression: function as an expression of an item, appears in the expression, with the function return value to participate in the operation of the expression. ( This method requires that the function has a return value )
2. Function statements: functions can perform only certain operations without returning function values. (a function call can be used as a stand-alone statement.) )
3. Function parameters:The function appears as an actual argument to another function call. (This is the case where the return value of the function is passed as an argument and therefore requires that the function must be a return value)
1.4. The function's form participates in the actual parameter:
The parameters of the function are divided into formal parameter and real parameter, which is to realize data transmission.
The formal parameter appears in the function definition and can only be used within the function body. When a function call occurs, the calling function copies the value of the argument 1 copies to the parameters of the called function, thus implementing the calling function's data transfer to the called function.
"Case" argument data transfer to formal parameters
/* Data passing of parameters to parameters */
static void S (int n)
{
int i;
Console.WriteLine (n);
for (i=n-1;i>=1;i--)
n=n+1;//changing the value of a formal parameter
Console.WriteLine (n);//The value of the parameter after the output change
}
static void Main (string [] args)
{
int n=10;//defines the argument N and initializes
s (n);//Call function
Console.WriteLine (n);//See if the argument is changed by a function call
Console.ReadLine ();
}
Description:1. Arguments can be constants, variables, expressions, functions, etc. regardless of the type of argument, they must have a definite value when making a function call to pass the values to the parameter. (therefore, an assignment, input, and so on should be used beforehand to obtain a definite value for the argument)
2, the shape parametric only when is called, only then allocates the internal memory cell, at the end of the call, immediately releases the allocated unit.
3, the parameters of the parameters of the data is one-way, that is, only the value of the actual parameter is passed to the formal parameter, but not the value of the formal parameter is passed to the argument in reverse.
4, arguments and parameters occupy different memory units, even if the same name does not affect each other.
2. Array as function parameter:
An array is used as a function parameter in two ways:one is to use the array element as an argument, and the other is to use the array name as the parameter and argument of the function.
2.1. Array elements as function parameters:
The array element is the subscript variable, which is no different from the normal variable.
An array element can only be used as a function argument, and its usage is exactly the same as a normal variable: When a function call occurs, the value of the array element is passed to the parameter for one-way value passing.
"Case" write a function that counts the number of letters in a string
static bool Isalp (char c)
{
if (c>= ' a ' &&c<= ' Z ' | | c>= ' A ' &&c<= ' Z ')
{
return true;
}
Else
{
return false;
}
}
static void Main (string [] arge)
{
int i,num=0;//declaring a variable
String Line=console.readline ();//Receive user input
Char[] Str=line. ToCharArray ();//Convert a string to a character array
int LINELEN=STR. length;//get the length of the character array
for (i=0;i<linelen;i++)
{
if (Isalp (Str[i]))
{
num++;
}
}
Console.WriteLine (num);
Console.ReadLine ();
}
Description:1, when using array elements as arguments, as long as the array type and function of the formal parameter type is consistent, does not require the function of the formal parameter is subscript variable. In other words, the processing of an array element is treated as a normal variable.
2, when the normal variable or subscript variable as a function parameter, the parameter variables and arguments are two different memory units allocated by the compilation system. The passing of the value that occurs when the function is called, is to assign the value of the argument variable to the parameter variable.
2.2, the array name as a function of formal parameters and arguments:
Unlike array elements as formal parameters and arguments to functions, when an array element is used as an argument, the processing of the arrays is a pass-through reference (address).
That is, when the array is a function parameter, the parameter variables and argument variables are the two identical memory units allocated by the compilation system. The address transfer that occurs when the function is called is to assign the address of the argument to the parameter variable.
The Member method of------------class is actually a function, which can be divided into general member methods and constructors and destructors. ------------
function (handwriting)