zoukankan      html  css  js  c++  java
  • C++参数的默认值

    1,函数声明(.h)不要指定形参的默认值,在函数定义中指定。

    2,指定了一个形参的默认值,后面的形参全都要指定默认值。

    3,调用函数时,如果未传递参数的值,则会使用默认值,如果指定了值,则会忽略默认值,使用传递的值。如果实际参数的值留空,则使用这个默认值。

    #include <iostream>
    using namespace std;
     
    int sum(int a, int b=20)
    {
      int result;
    
      result = a + b;
      
      return (result);
    }
    
    int main ()
    {
       // 局部变量声明
       int a = 100;
       int b = 200;
       int result;
     
       // 调用函数来添加值
       result = sum(a, b);
       cout << "Total value is :" << result << endl;
    
       // 再次调用函数
       result = sum(a);
       cout << "Total value is :" << result << endl;
     
       return 0;
    }
  • 相关阅读:
    数组字典
    字符串创建
    不可变数组
    数组
    纯净控制台
    可变字典
    数组Array
    id NSObject instanceType 区别
    可变字符串 插入,删除,替换,赋值
    查询字符串中某部分的字符串
  • 原文地址:https://www.cnblogs.com/afreeman/p/8564206.html
Copyright © 2011-2022 走看看