In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the C++ function parameter example analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone will have some gains after reading this C++ function parameter example analysis article, let's take a look at it.
Default values of function parameters
In C++, you can provide a default value for a parameter at function declaration time.
Default values are used when a function is called without providing a value for the parameter
Default values for parameters must be specified in the function declaration
Here is a code:
#include int mul(int x = 0);int main(int argc, char *argv[]){ printf("%d\n", mul()); printf("%d\n", mul(-1)); printf("%d\n", mul(2)); return 0;}int mul(int x){ return x * x;}
The following are the compilation results of the program in C++ and C language environments respectively:
In C++ compiler environment, the program can be compiled, but not in C language conditions, which shows that the default value of function parameters is an extension of C++ language to C language. And the default value of the function parameter will not be reported when it is defined, if it is placed in the function declaration, it will report an error.
Rules for default parameters of functions
Default values for parameters must be supplied from right to left
Function calls with default values, subsequent arguments must use default values
As follows:
Here's a code to feel:
#include int add(int x, int y = 0, int z = 0);int main(int argc, char *argv[]){ printf("%d\n", add(1)); printf("%d\n", add(1, 2)); printf("%d\n", add(1, 2, 3)); return 0;}int add(int x, int y, int z){ return x + y + z;}
The output is as follows:
II. Function placeholder parameters
In C++ you can provide placeholder parameters for functions
Placeholder parameters have only parameter type declarations and no parameter name declarations
Usually, in letters.| Placeholder parameters cannot be used inside the number volume
That is, the following notation is not problematic in C++:
The Meaning of Function Placeholder Parameters
Placeholder parameters are used in combination with default parameters
Possible irregularities in compatible C programs
For example, the following two expressions mean the same thing in C++, indicating no parameter input; while in C, the former indicates that the number of parameters that can be input is unlimited, while the latter indicates no parameter input.
Here is a code:
#include int func(int x, int = 0);int main(int argc, char *argv[]){ printf("%d\n", func(1)); printf("%d\n", func(2, 3)); return 0;}int func(int x, int){ return x;}
The following is the output of the code under C++ and C compilation conditions:
About "C++ function parameter example analysis" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "C++ function parameter example analysis." If you still want to learn more knowledge, please pay attention to the industry information channel.
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.