ArticleDirectory
Chapter 4 Functions
Many people are still confused about what functions and methods are. In fact, these two concepts are the same thing. Methods are the product of object-oriented. For example, we say that a method of an object is used, and functions are the product of process-oriented. For example, we often say that a function is executed. Both functions and methods are written in the same way. This chapter will introduce the related concepts of functions in detail.
The focus of this chapter:
◆ Concept of function Overloading
◆ Advantages of function Overloading
4.3 function Overloading
Function overload isProgramDevelopment provides great convenience, especially for the users of these class libraries-they can be freed from complicated type conversions, as long as they call methods in the most convenient way we think. This section mainly discusses some basic conditions related to the use of overload from the perspective of the syntax of overload.
4.3.1 concept of function Overloading
Function overloading means that multiple function implementations can use one function name at the same time, but the parameter table of the function is different. For example, we can use overload functions to define multiple addition functions to calculate the sum of two numbers. One function implements the sum of two int types, the other implements the sum of two float types, and the other implements the sum of two decimal types. Each implementation corresponds to a function body. These functions have the same name, but their parameter types are different. This is the concept of function overloading.
We just mentioned that when a function is overloaded, multiple functions use the same function name. Then, the C # Language compiler needs to determine which function the user calls, that is, which function is used for implementation. When determining the function implementation, the number and type of function parameters must be distinguished. This means that during function overloading, functions with the same name are required to have different numbers of parameters or different parameter types. Otherwise, the overload cannot be implemented.
The format of function overload is as follows:
Void add (int A, int B)
{}
Void add (int A, int B, int C)
{}
Char add (char a, char B, char C)
{}
In the preceding format, three add functions are defined. The first add () function corresponds to the implementation of two int type summation functions, while the last one is add () the function corresponds to the implementation of the three int-type summation functions. The last add () function corresponds to the implementation of the three char-type summation functions, which is the overload of the function.
Here is an example:
Using system;
Namespace Microsoft. Example
{
Public class testreturnvalue
{
Static int max (int x, int y) // defines the max function of the int version.
{
Int Z;
Z = x> Y? X: Y; // compare the size of X and Y.
Return Z;
}
Static double max (Double X, Double Y) // defines the max function of the double version.
{
Double Z;
Z = x> Y? X: Y; // compare the size of X and Y.
Return Z;
}
Static void main (string [] ARGs)
{
Int intx = 50; // defines an int variable.
Int inty = 43; // defines an int variable.
Console. writeline ("compare the two integers:" + intx + ";" + inty );
Int intresult = max (intx, inty); // call the max function of the int version.
Console. writeline ("Result:" + intresult );
Double doublex = 50.5; // defines a double variable.
Double doubley = 43.3; // defines a double variable.
Console. writeline ("compare the two integers:" + doublex + ";" + doubley );
Double doubleresult = max (doublex, doubley); // call the max function of the int version.
Console. writeline ("Result:" + doubleresult );
}
}
}
Above Code In row 6th, we define an int version of the Max function. This function only receives two int type parameters and then returns int type data. In row 12th, we define a double-type Max function. This function only receives two double-type parameters and then returns double-type data. These two functions use the max function name, but they can coexist, which is the function overload.
The final output result is:
Compare the values of two integers: 50; 43
Result: 50
Compare the values of two integers: 50.5 and 43.3.
Result: 50.5
4.3.2 advantages of function Overloading
From the above example, we can know that although different loads are multiple independent functions in form, they represent the same function in semantics-to be precise, they perform the same operation. The main purpose of a function to provide multiple Overloading is to facilitate the caller. Specifically, it includes the following advantages:
1. Support for Multiple Data Types
The previous example of the Max function illustrates this. We want to provide the function of "getting the maximum value", instead of "getting the absolute value of an integer". Therefore, it is inappropriate to limit the input parameter to an integer. By providing overloading, you do not have to worry about data type conversion. Each overload function processes different data types and reduces memory.
2. Support multiple data provision Methods
This is more extensive and convenient than supporting multiple data types. INT and double types can be directly converted, but in many cases it is not that simple. For example, the xmldocument class provides the load method for loading an XML document. How do I specify this document? If this document has not been opened, it is most convenient to specify its path and file name. If this document has been opened, it may be most convenient to access it through a stream object. For users of class libraries, these may all exist. If xmldocument enforces a method, it will undoubtedly cause inconvenience to users. The best way to do this is to provide a set of reloads:
Xmldocument. Load (string)
Xmldocument. Load (Stream)
Here, there is no type conversion relationship between string and stream, and the extra work of the overload function is not as simple as the previous maximum function. But for function users, their meanings are similar.
3. provide default values for complex parameters to simplify calls
This is also a very common situation. Some functions are very powerful, but the negative impact is that function callers need to consider too many parameter settings. In fact, in most cases, the user only cares about one or two parameters, and the remaining parameters are concentrated on some of the same set values. If you can provide reasonable default values for these advanced features, the workload of function callers will be greatly reduced. For example, the create method of file is used to create a file. It supports many set parameters:
Public void create (
String path;
Int buffersize;
Fileoptions;
Filesecurity;
}
With so many parameters, we will be overwhelmed when creating a file for the first time. As we understand it, it is enough to specify the file address. As shown below:
Public void create (
String path;
}
In fact, create does provide such a simple version of overload. In most cases, users only need some simple functions, and those complex features are used less frequently. If the caller needs to set so many parameters each time, it obviously adds a lot of unnecessary work. If you provide a simple version overload that provides reasonable default values for these advanced features, you can make the code more concise and orderly. The previous overload function may be implemented in the following way:
Public void create (string path)
{
Create (
Path;
0
Fileoptions. None
Null
);
}
You can clearly see how much code you need to write to complete a simple operation without these overloading to help set the default parameter values.
Author's experience:
From the previously mentioned overload semantics, it is not difficult to find that the differences between each overload of a function are only the preprocessing of entry parameters, and their core functions are consistent. If each overload is written separately, a large amount of redundant code will inevitably be generated, which not only increases the coding work, but also creates great difficulties for future modification and maintenance. Since the overload function is provided only to process different entry parameters, the work of each overload function should be to process the parameters. The core function should be implemented only by one function, all other overloading calls this function.