标准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 函数 不需要返回语句
}

 运行结果:

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

 

 

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;
}

运行结果:

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

 

相关文章:

  • 2021-10-15
  • 2021-12-06
  • 2021-12-16
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2021-12-19
  • 2021-07-25
猜你喜欢
  • 2022-01-09
  • 2021-05-19
  • 2021-06-26
  • 2021-12-12
  • 2022-12-23
  • 2021-10-22
  • 2021-12-03
相关资源
相似解决方案