In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the functions in C++. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I. Foundation
Function: encapsulates a piece of code that can be called repeatedly during a single execution, including the function header and the function body
Function header:
Function name (identifier) for subsequent calls
Formal parameters, representing the input parameters of the function
Return type, the type of the result returned after the completion of the function execution
Function body: a block that contains specific computational logic
Declaration and definition of functions:
The function declaration contains only the function header, not the function body, usually in the header file
Function declarations can occur multiple times, and definitions usually appear once (there are exceptions)
Function call:
Function name and actual parameters need to be provided
The actual parameter copy is initialized to the formal parameter
The returned value is copied to the caller of the function
Stack frame structure (you can understand it by yourself)
Second, parameters
For non-template functions, each parameter has a definite type, but the parameter can have no name
The copy order of actual parameters to formal parameters is uncertain.
The transmission of formal parameters of a function is generally divided into: passing value, passing address, passing reference.
Definition of variable length parameter:
1. Use initializer_list to pass
# include void fun (std::initializer_list a) {} int main {fun ({1,2,3,4})}
Note: this method can only pass variable length parameters of the same type.
Variable length template parameters
Use ellipses to indicate formal parameters (generally not used)
Note the default arguments of the function:
1. If a parameter has a default parameter, then the parameter on its right must have a default parameter
Void fun (int xylene 1, int yearly 2) {} / / where y must be given a default value
2. When a function call with default arguments is called, the passed arguments are matched from left to right
3. In a translation unit, the default argument of each parameter can only be defined once.
4. When the default argument is an object, the default value of the argument will change with the object value.
The version of the main function:
Invisible reference version (for general use)
With parameter version
Int main (int argc, char * argv []) {}
Argc is a non-negative number that represents the number of parameters passed in, and argv is an array header that points to the transfer parameters.
3. Return type
Several writing methods of the return type:
Classical method: at the front of the function head, which is also the most conventional way to write it.
The mode introduced by Category 11: located at the end of the function header
Auto fun (int x)-> int {return x;}
The method introduced by Category 14: automatic derivation of return types
Auto fun (int a) {return a; / / will derive according to the return statement} IV. Function overloading and parsing
Function overloading: define multiple functions with the same function name, each with a different parameter list
Note: cannot be overloaded based on different return types
Name Lookup:
It is divided into qualified search and unqualified search: whether it is limited in a certain scope or not.
Unqualified lookup will do a step-by-step search of the domain-name hiding.
Lookups are usually done only in a collection of declared names
Overload resolution: further select the appropriate calling function on the basis of name lookup
Filter the version that cannot be called: the number of parameters is incorrect, the argument cannot be converted into a formal parameter, and the argument does not meet the restrictions of the parameter.
5. Inline function
Definition: expand the relatively simple function logic to the part of the calling function to avoid stack frame destruction and improve performance
Keyword: inline, if a function is expanded in multiple translation units, adding this keyword can avoid repeated definitions
1.constexpr function
Definition: this keyword was used in the previous introduction to constant expressions, and can now be used for functions as well
Function: causes the function to be executed in the compiler and, of course, at run time in the case of variables
Constexpr int fun (int x) {/ / int y; std::cin > > y; will report an error. This statement requires the user to pass in parameters and can only execute return x * 2 at run time;} int main {constexpr int x = fun (2); / / the compiler will translate to move eax 4, and you can return x without constexpr;}
Note: the statements in the constexpr function must be statements that can be executed in the compiler
Extension: keyword consteval (introduced by Clipper 20), functions can only be executed in the compiler
VI. Function pointer
Function: can be used in higher-order functions, using function pointers as parameters
Code case:
Int add (x) {return x + 1}; using T = int (int); int fun (K* F, int x) {int tmp = (* F) (x); return tmp * 2;} int main {std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.