cpp 7.1

本文通过两个示例介绍了C++中如何定义和调用函数,包括简单的无返回值函数及带有计算并返回结果的函数。第一个示例展示了如何声明和调用一个无返回值的简单函数;第二个示例则进一步介绍了如何创建一个接收参数并返回计算结果的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7.1

#include<iostream>
void simple();

int main()
{
	using namespace std;
	cout << "main() will call the simple() function:\n";
	simple();
	cout << "main() is finished with the simple() function.\n";

	system("pause");
	return 0;

}
void simple()
{
	using namespace std;
	cout << "I'm but a simple function.\n";
}


7.2

#include<iostream>
void cheers(int);
double cube(double x);
int main()
{
	using namespace std;
	cheers(5);
	cout << "Give me a number: ";
	double side;
	cin >> side;
	double volume = cube(side);
	cout << "A" << side << "-foot cube has a volume of ";
	cout << volume << " cubic feet.\n";
	cheers(cube(2));

	system("pause");
	return 0;
}

void cheers(int n)
{
	using namespace std;
	for (int i = 0; i < n; i++)
		cout << "cheers! ";
	cout << endl;

}

double cube(double x)
{
	return x*x*x;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值