1. 程式人生 > >VS 2017 簡單的使用者定義函式及呼叫

VS 2017 簡單的使用者定義函式及呼叫

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

執行結果: