C + + default parameter value function

Source: Internet
Author: User
Tags function definition

1. Functions for default parameter values

The C + + language allows you to specify a default value for a function when it is defined or some form parameter (parameter) by writing "= Default" after the corresponding parameter, and if the value of the argument at the corresponding location is omitted, the default value of the parameter is evaluated when the function is executed.

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

2. Default parameter value function considerations

    • The default function is typically set in the function declaration. If there are function declarations and function definitions in the program, the default values of the parameters are not allowed to be defined, and if there are only function definitions in the program and no function declarations, the default parameters can appear in the function definition;
    • The order of the default parameters. If there are multiple default parameters in a function, the default parameters in the parameter distribution should be defined individually from right to left. That is, if you want to give the default value of this parameter to the right side of the parameter, then the definition is wrong. Such as:

void int a=1floatlong c= );    // Error void int float b=2long c= );    // correct

Example: Define and test overloaded functions.

#include <iostream>using namespacestd;intSumintIintj=Ten);//there is a function declarationintSumintIintj) {    returni+J;}voidPrintintIintJintsum=0)//no function declaration{cout<<"I:"<< I <<Endl; cout<<"J:"<< J <<Endl; cout<<"sum:"<< sum <<Endl;}intMain () {intA=3; intb=7; cout<<"sum (a):"<< sum (a) <<Endl; cout<<"sum (A, b):"<< sum (A, B) <<Endl; cout<<"print (A, b):"<<Endl;    Print (A, b); cout<<"Print (A,b,sum (b)):"<<Endl;    Print (A,b,sum (A, b)); return 0;}//Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!! 

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

C + + default parameter value function

Related Article

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.