The concept of a function: a function is a small, independent program defined in a class that has a specific function. (Functions are also called methods)

Definition of function: Function can help us to functional, logical code packaging, so as to achieve repeated calls, improve the program reuse and efficiency.

Define the format of the function:

Modifiers return value type function name (parameter type parameter1, parameter type, parameter2. Parameter type, parameter n) {execute statement;returnThe return value. }Copy the code
  • Function name: The name of the function, which must be named according to the naming convention to indicate the function’s function.
  • Return value: represents the final result of this function.
  • Return value type: Depends on the type of the return value.
  • Return: action that returns the return value, returning the return value to the caller; It also closes the function.
  • Argument list (in parentheses) : Defines the unknown data that this function needs to operate on, and each data has a data type that can be understood as a variable.
  • Modifiers: Modifies what this function is. There can be more than one modifier.
  • Function body: The body of a function’s operation, with braces indicating the scope.

Function usage: we commonly use two methods: direct call, assignment call.

Note: The return value of a function depends on the function’s function. If there is no return value, then the return value type is void. Void is also the key. Function arguments are actually evaluated by the actual arguments passed in to the call, and should be able to distinguish between argument and parameter names.

Function features:

  • Defining functions encapsulates functional code.
  • Defining a function makes it easy to reuse that functionality.
  • The advent of functions improves code reusability.
  • Functions are not executed until they are called.

If the function does not have a specific return value, the return value type is void. Then the return statement in the function can be omitted.

Return has two functions: 1, returns a value. 2 End the function. If there is a return value, then the return keyword is essential, and note that multiple branches must specify return.

Functions can only be called from within a function, and cannot be defined within a function. When a function is defined, the result of the function should be returned to the caller for processing

Overloading of functions:

Concept: Overload: More than one function of the same name can exist in the same class, as long as they take different arguments or arguments of different types.

public int add (int x, int y)// Get the sum of two integers
{
    return x + y;
}

// Get the sum of two decimals
public double add(double x, double y)
{
    return x + y;
}

// Get the sum of three integers
public int add (int x,int y, int z)
{
    return x + y + z;
}
Copy the code

 

class Demo
{
    static voidMain(string[] args)
    {
        Add( 1.2 ,1 ); // The third parameter, 1, will replace the difference
        Add( 1.2 ); // If the third parameter does not exist, an optional parameter is entered by default
    }

    static int Add(inta,int b ,int c = 0 ) // How to write optional parameters
    {
        System.Console.Print("a :{0},b:{1},c:{2} ",a,b,c); }}Copy the code

The difference between overloading and optional arguments:

Optional arguments, which specify a default value when declaring an argument to a method, do not have to be specified if the default value is used when calling the method. In other words, when this parameter value is not explicitly specified, the function automatically uses the preset default value.

Examples of optional parameters:

All optional arguments must be written to the right of the normal argument. If written to the left of the normal argument, the compiler will report an error

Analysis of overloaded matching principle:

  1. When the caller has overloaded functions in the matching class, it first finds all overloaded functions that match the number of arguments.
  2. Find the overloaded function with the best matching type given the number of parameters. If not, implicitly convert it to see if it is responsible for matching.
  3. If more than one overloaded function meets the condition, the overloaded function with no optional parameters is preferred.

Overloaded instances:

class Demo
{
    static voidMain(string[] args)
    {

        Add(1.2.1);
        Add(1.2.5.0 f);
        Add(1.0 f.2.0 f.5.0 f);
        Add(1.2);

        System.Console.ReadLine();
    }

    static void Add(int a,int b)
    {
        System.Console.WriteLine("First function called!");
        System.Console.WriteLine("a :{0} ,b :{1}, ", a, b );
    }


    static void Add(int a,int b, int c = 0)
    {
        System.Console.WriteLine("The second function is called!");
        System.Console.WriteLine("a :{0} ,b :{1},c :{2} ", a, b, c);
    }

    static void Add(int a,int b, float c)
    {
        System.Console.WriteLine("The third function is called!");
        System.Console.WriteLine("a :{0} ,b :{1},c :{2} ", a, b, c);
    }

    static void Add(doublea, double b, double c)
    {
        System.Console.WriteLine("The fourth function is called!");
        System.Console.WriteLine("a :{0} ,b :{1},c :{2} ", a, b, c); }}Copy the code