VS 2017 C++ 简单的用户定义函数及调用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zaishuiyifangxym/article/details/82934014

标准C库提供了140多个预定义函数,如果其中的函数能满足要求,则应调用这些函数

(如求平方根函数,直接调用 sart(Variable name); 即可)。但有时候,用户需要编写自己的函数,尤其在设计类的时候。

 调用自己编写的函数 可分为:有返回值 函数 和 无返回值 函数;下面依次介绍:

1、 定义无返回值函数,并调用;

下面直接放代码,更直观。

#include <iostream>

void simon(int); //funtion 

int main() //主函数
{
	using namespace  std;

	/*simon(3);
	cout << "Pick an integer: ";*/

	int count;
	cin >> count;
	simon(count);
	cout << "Done! " << endl;

	cin.get();
	cin.get();
	
}

void simon(int n) //定义的无返回值函数
{
	using namespace std;
	cout << "Simon saya touch your toes "
		<< n
		<< " times."
		<< endl;
	// void 函数 不需要返回语句
}

 运行结果:

2、定义有返回值函数,并调用;

#include <iostream>
int stonetolb(int);

int main() 
{
	using namespace std;
	int stone;
	cout << "Enter the weight in stone: ";
	cin >> stone; // input 

	int pounds = stonetolb(stone);
	cout << stone
		<< " stone = ";
	cout << pounds
		<< " pounds. "
		<< endl;

	cin.get();
	cin.get();
	return 0;

}

int stonetolb(int sts)  // 定义有返回值的函数
{
	return 10 * sts;
}

运行结果:

猜你喜欢

转载自blog.csdn.net/zaishuiyifangxym/article/details/82934014
今日推荐