Home page of personal blog (click to view details)-- https://blog.51cto.com/11495268
1, introduction
In C + + language, functions are needed for everything. This paper introduces the basic concepts related to functions, and understands their existence -- function definition, function call, function overload and function template (as for the special class functions in C + +, this article will not introduce them for the moment)
2. Function definition
//When defining a function, the parameters in parentheses after the function name are parameters Return value type function name ([parameter]) { Function body }
3. Function call
// When a function is called, the parameters in parentheses after the function name are arguments #include <iostream> using namespace std; void exchange_num(int *first_num, int *sec_num) { int exchange_tmp = 0; exchange_tmp = *first_num; *first_num = *sec_num; *sec_num = exchange_tmp; } int main(void) { int first_num = 1; int sec_num= 10; // Calling function exchange_num(&first_num, &sec_num); cout << "first_num:" << first_num << endl; cout << "sec_num:" << sec_num << endl; return 0; }
4. Function overload
C + + allows multiple functions to be defined by the same function name. Overloaded functions must have different parameter numbers or parameter types
// When a function is called, the parameters in parentheses after the function name are arguments #include <iostream> using namespace std; void exchange_num(int *first_num, int *sec_num) { int exchange_tmp = 0; exchange_tmp = *first_num; *first_num = *sec_num; *sec_num = exchange_tmp; } void exchange_num(double *first_num, double *sec_num) { double exchange_tmp = 0; exchange_tmp = *first_num; *first_num = *sec_num; *sec_num = exchange_tmp; } int main(void) { int ifirst_num = 1; int isec_num= 10; double dfirst_num = 1.1; double dsec_num = 10.01; // Calling function exchange_num(&ifirst_num, &isec_num); exchange_num(&dfirst_num, &dsec_num); cout << "ifirst_num:" << ifirst_num << endl; cout << "isec_num:" << isec_num << endl; cout << "dfirst_num:" << dfirst_num << "\ndsec_num:" << dsec_num << endl; return 0; }
5. Function template
When overloading functions, it can be found that some overloaded functions are only of different parameter types. In C + + language, there are many data types, so it is not necessary to write an overloaded function for each type (theoretically, this is feasible), but it causes a lot of repeated code. Function template is just to solve this problem. It is recommended to use general functions when calling functions , the system will replace the virtual type in the template according to the type of arguments, so as to realize the functions of different functions
// When a function is called, the parameters in parentheses after the function name are arguments // Template function call does not support implicit type conversion #include <iostream> using namespace std; // The class keyword can also be replaced with the typename keyword template <class c2> void exchange_num(c2 *first_num, c2 *sec_num) { c2 exchange_tmp = 0; exchange_tmp = *first_num; *first_num = *sec_num; *sec_num = exchange_tmp; } int main(void) { int ifirst_num = 1; int isec_num= 10; double dfirst_num = 1.1; double dsec_num = 10.01; // Calling function exchange_num(&ifirst_num, &isec_num); exchange_num(&dfirst_num, &dsec_num); cout << "ifirst_num:" << ifirst_num << endl; cout << "isec_num:" << isec_num << endl; cout << "dfirst_num:" << dfirst_num << "\ndsec_num:" << dsec_num << endl; return 0; }