1. 程式人生 > >使用函式組織程式碼

使用函式組織程式碼

前面我們學習的都只有一個main函式,但是當代碼比較長時,應使用函式的呼叫,使程式碼更具有邏輯性、可讀性。

函式的編寫型別

函式的編寫有很多種方法,下面介紹幾種常用方法,分別用例子來表述:
1 帶有多個引數的函式
計算圓柱的面積

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
double  cylinderArea(double radius, double height)
{
	return 2 * radius*radius*pi + 2 * radius*height;
}
int main()
{
	double inradius = 0;
	cout << "Please input a radius : ";
	cin >> inradius;
	double inheight = 0;
	cout << "Please input a height : ";
	cin >> inradius;
	cout<<"the cylinderArea is : "<<cylinderArea( inradius, inheight)<<endl;
	system("pause");
    return 0;
}

2 沒有返回值和引數的函式(這個比較簡單)
3 帶有預設引數的函式
該方法在定義時預設引數pi,如果不輸入形參,在呼叫時預設使用定義時的引數


#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

double  cylinderArea(double radius, double height,  double pi = 22 / 7.0)
{
	return 2 * radius*radius*pi + 2 * radius*height;
}
int main()
{
	double inradius = 0;
	cout << "Please input a radius : ";
	cin >> inradius;
	double inheight = 0;
	cout << "Please input a height : ";
	cin >> inradius;
	cout << "Do you want to change the pi (y/n)";
	char changelatter = '\0';
	cin >> changelatter;
	if (changelatter == 'y')
	{
		cout << "Please input the newpi : ";
		double newpi = 0;
		cin >> newpi;
		cout << "the cylinderArea is : " << cylinderArea(inradius, inheight,newpi) << endl;
	}
	else
		cout << "the cylinderArea is : " << cylinderArea(inradius, inheight) << endl;
	system("pause");
    return 0;
}

4 可以呼叫自己的函式-遞迴函式
求出斐波那契數列,對比前面的巢狀迴圈求解https://mp.csdn.net/mdeditor/83987253

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

int fibonacci(int num)
{
	if (num < 2)
		return num;
	else
		return fibonacci(num-1) + fibonacci(num-2);
}
int main()
{
	int innum = 0;
	cout << "Please input a num : ";
	cin >> innum;
	int index = 0;
	for (index=0;index<innum;index++)
		cout << "the fibonacci is : " << fibonacci(index) << endl;
	system("pause");
    return 0;
}

在這裡插入圖片描述
5 包含多條return語句的函式

使用函式處理不同型別的資料

1 將陣列傳遞給引數
先來對比在定義時傳遞一個引數與傳遞陣列的區別
void inputnumber(int numbers);
void inputnumber(int numbers[],int length);

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

void inputnumber(int number[], int length)
{
	int index = 0;
	for (index = 0; index < length; index++)
	{
		cout << "the number is : " << number[index] << endl;
	}
}
int main()
{
	
	int innumber[5] = {1,2,3,4,5};
	inputnumber(innumber, 5);
	system("pause");
    return 0;
}

2 函式過載
即具有相同的名稱和返回型別的函式,引數可以不同。
用函式的過載求解圓形和圓柱的面積

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
double circleorcylinderArea(double radius)
{
	return radius * radius*pi;
}
double circleorcylinderArea(double radius, double height)
{
	return 2 * circleorcylinderArea(radius) + 2 * pi*radius*height;
}
int main()
{
	cout << "please input  a radius : ";
	double inradius = 0;
	cin >> inradius;
	cout << circleorcylinderArea(inradius) << endl;
	cout << "please input  a height : ";
	double inheight = 0;
	cin >> inheight;
	cout<<circleorcylinderArea(inradius, inheight)<<endl;
	system("pause");
	return 0;
}

在這裡插入圖片描述
記錄一個錯誤:在函式return返回值後,誤以為直接呼叫可以有值的輸出,導致執行沒有值的輸出。所以定要用cout命令輸出到視窗, cout<<circleorcylinderArea(inradius, inheight)<<endl。
3 按引用傳遞引數
運用&告訴編譯器,不要將實參複製給函式,而是將指向該實參的引用傳遞給函式。
以上一個求解的面積為例子

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
void circleorcylinderArea(double radius,double & result)
{
    result=radius * radius*pi;

}
void circleorcylinderArea(double radius, double height,double & result)
{
	result= 2 * radius*radius*pi+ 2 * pi*radius*height;
}
int main()
{
	cout << "please input  a radius : ";
	double inradius = 0;
	cin >> inradius;
	double outresult = 0;
	circleorcylinderArea(inradius, outresult);
	cout << "the circlrArea is " << outresult << endl;
	cout << "please input  a height : ";
	double inheight = 0;
	cin >> inheight;
	circleorcylinderArea(inradius, inheight, outresult);
	cout << "the cylinderArea is " << outresult << endl;
	system("pause");
	return 0;
}

注意:
1 在運用指向實參的引用傳遞引數時,函式沒有返回值,所以為void型別
2 直接用cout輸出引用指向的結果引數,例如:cout << "the cylinderArea is " << outresult << endl;
要和上面 cout<<circleorcylinderArea(inradius, inheight)<<endl;
直接呼叫引數並輸出區分開來。