The extension of function parameters in the 7th lesson

Source: Internet
Author: User
Tags mul

1. default values for function parameters

(1) C + + can provide a default value for a parameter when a function is declared (note that the declaration is not available in the definition )

(2) When a function call does not provide a value for the parameter, the default value is used

Preliminary study on default parameter value of "instance analysis"

#include <stdio.h>//The default value can only be provided when a function declarationintMulintx =0);//the default value for parameter x is 0intMain () {printf ("%d\n", Mul ());//incoming default value 0printf"%d\n", Mul (-1));//Incoming-1printf"%d\n", Mul (2));//Incoming 2    return 0; }intMulintX//definition, cannot provide default value, compiler will error{    returnX *x;}

(3) Rules for default values of function parameters

declaration , the default value must be supplied from right to left

When the ② function is called , if a default value is used, subsequent arguments must use the default value .

Error example of the "programming experiment" default parameter

#include <stdio.h>//default parameters must be supplied from right to left, such as//int Add (int x = 0,int y = 1,int z) is wrongintAddintXinty =1,intz =2);intMain () {//The 2nd parameter y uses the default value, then the subsequent z must also use the default value//such as Add (1,, 3), the call is wrong. printf"%d\n", Add (0));//x = 0, y = 1, z = 2printf ("%d\n", Add (2,3));//x = 2, y = 3, z = 2printf"%d\n", Add (3,2,1));//x = 3, y = 2, z = 1    return 0; }intAddintXintYintz) {    returnX + y +Z;}

2. Function-placeholder parameters in C + +

(1) The placeholder parameter has only the parameter type declaration, and no parameter name declaration, such as: int func (int x,int)

(2) Under normal circumstances, the placeholder parameter cannot be used inside the function body

(3) Significance of placeholder parameters

The ① placeholder parameter is used in conjunction with the default parameter .

② compatible C language programs may appear in the non-standard wording

"Programming Experiment" placeholder parameters and default parameter values

#include <stdio.h>//The int func () in C can accept arbitrary arguments, so in subsequent calls it may//There are different invocations such as func (1), Func (2, 3), and such code is in C + +//wrong, so in order to be compatible with the C language of this nonstandard notation, you can provide Func with two//The two preceding calls are legal if the take-up parameter such as func (int = 0,int = 0)//at a very low cost, you can make C code available in C + +. Makes you feel like a fake.//Buddha C + + can also be like C language, accept any parameter! //placeholder parameter, and the default value is 0intFuncintx =0,int=0);intMain () {printf ("%d\n", func ());//0printf"%d\n", Func (1));//1printf"%d\n", Func (2,3));//2    return 0; }//The 2nd parameter is a placeholder parameter (no function name), so it cannot be used inside a function//This parameter only plays a role in the placeholderintFuncintXint){    returnx;}

3. Summary

(1) Default value for function parameters supported in C + +

(2) If a function call does not provide a parameter value, the default value is used

(3) The default value of the parameter must be supplied from right to left

(4) If a default parameter is used when the function is called, the following parameter must use the default value

(5) Support for placeholder parameters in C + + for compatibility with non-canonical wording

The extension of function parameters in the 7th lesson

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.