1. 程式人生 > >C++自學-默認參數的函數

C++自學-默認參數的函數

OS using span () ont mgr pre 全部 win

在調用函數時,可以不用或使用部分參數調用,不指定的值為函數默認值

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <shappmgr.h>
#include <stdio.h>
#include <cmath>

using namespace std;

int add(int a = 2, int b = 4, int c = 6) {
	return a + b + c;
}
int main() {
	int x=add();//全部使用默認參數
	cout << x << endl;
	int y = add(1, 3);//C的值使用默認參數
	cout << y << endl;
	int z=add(1, 3, 5);//全部不使用默認值
	cout << z << endl;
	cin.get();
}

  

C++自學-默認參數的函數