C++函数默认参数和占位参数

// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

void func(int a=666)
{
	cout << "a=" << a << endl;
}
//求立方体体积
void  get(int len, int w, int h=10)
{
	cout << len << " " << w << "  " << h << endl;
	cout << len * w*h << endl;
}
int main()
{
	int value = 110;
	func();//得到的结果  打印是666
	int len = 1;
	int w = 2;
	int h = 3;
	//get(len, w, h);
	//默认参数是从右向左,如果len有默认参数,那么w和h也要有
	get(len, w);
	return 0;
}

	

在这里插入图片描述
占位参数

// h_mingcheng.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

void func(int a=666)
{
	cout << "a=" << a << endl;
}
//求立方体体积
void  get(int len, int w, int h=10)
{
	cout << len << " " << w << "  " << h << endl;
	cout << len * w*h << endl;
}
void fun(int x,int)//占位参数
{
	cout << "x=" << x << endl;
}
void fun1(int x, int = 0)
{
	cout << "x=" << x << endl;
}
int main()
{
	int value = 110;
	func();//得到的结果  打印是666
	int len = 1;
	int w = 2;
	int h = 3;
	//get(len, w, h);
	//默认参数是从右向左,如果len有默认参数,那么w和h也要有
	get(len, w);
	fun(10, 20);
	fun1(10);

	return 0;
}

	
发布了35 篇原创文章 · 获赞 2 · 访问量 2429

猜你喜欢

转载自blog.csdn.net/weixin_41375103/article/details/104282533