C++函數(二)

C++函數(二)

1.函數的參數傳遞

(1)在函數被調用時才分配形參的存儲單元

(2)實參可以是常量、變量或表達式

(3)實參類型必須與形參相符

(4)值傳遞是傳遞參數值,即單項傳遞

(5)引用傳遞可以實現雙向傳遞

(6)常引用作參數可以保障實參數據的安全

2.引用類型

引用類型:

引用(&)是標識符的別名

例如:

int i,j;

int &ri=i;//定義int引用ri,並初始化爲變量i的引用

j=10
ri=j;//相當於i=j

一旦一個引用被初始化後,就不能改爲指向其他對象

引用可以i作爲形參

例:輸入兩個整數交換後輸出(值傳遞)
#include"iostream"
using namespace std;
void swap(int a,int b){
  int t=a;
   a=b;
   b=t;
} 
int main(){
 int x=5,y=10;
 cout<<"x="<<x<<"y="<<y<<endl;
 swap(x,y);
 cout<<"x="<<x<<"y="<<y<<endl;
 return 0;
}
運行結果:
x=5  y=10
x=5  y=10

3.含有可變參數函數

c++標準中提供了兩種主要方法

如果所有實參類型相同,可以傳遞一個名爲initializer_list的標準類型庫

如果實參的類型不同,可以編寫可變參數的模板(第九章)

initializer_list

initializer_list是標準庫類型,用於表示某種特定類型值的數組,該類型定義在同名的頭文件中4

例:計算長方體的體積
函數getVolume計算體積
由三個形參:length()width()height()其中width和height帶有默認值23
主函數中以不同形式調用getVolume函數
#include"iostream"
#include"iomanip"
using namespace std;
int getVolume(int length, int width = 2, int height = 3);

int main() {
	const int X = 10, Y = 12, Z = 15;
	cout << "Some box data is:";
	cout << getVolume(X, Y, Z) << endl;
	cout << "Some box data is:";
	cout << getVolume(X, Y) << endl;
	cout << "Some box data is:";
	cout << getVolume(X) << endl;
	return 0;
}

int getVolume(int length, int width, int height) {
	cout << setw(5) << length << setw(5) << width << setw(5)
		<< height << '\t';
	return length * width * height;
}
運行結果:
Some box data is:   10   12   15        1800
Some box data is:   10   12    3        360
Some box data is:   10    2    3        60
例題:編寫遞歸函數int fib(int n),在主程序中輸入n的值,調用fib函數計算Fibonacci級數。
公式爲:
fib(n)=fib(n-1)+fib(n-2),n>2;
fib(1)=fib(2)=1
#include"iostream"
using namespace std;
int fib(int n);
int main()
{
	int n, answer;
	cout << "Enter number:";
	cin >> n;
	cout << "\n\n";
	answer = fib(n);
	cout << answer << "is the " << n << "th Fibonacci number\n";
	return 0;
}
int fib(int n) {
	cout << "Processing fib(" << n << ")...";
	if (n < 3)
	{
		cout << "Return 1!\n";
		return (1);
	}
	else {
		cout << "Call fib(" << n - 2 << ")and fib(" << n - 1 << ")).\n";
		return (fib(n - 2) + fib(n - 1));
	
	}
}
運行結果:
Enter number:4


Processing fib(4)...Call fib(2)and fib(3)).
Processing fib(2)...Return 1!
Processing fib(3)...Call fib(1)and fib(2)).
Processing fib(1)...Return 1!
Processing fib(2)...Return 1!
3is the 4th Fibonacci number

4.函數重載

c++允許功能近似的函數在相同作用域內以相同函數名聲明,從而形成重載。

形參類型不同:

int add(int x,int y);

float add(float x,float y);

形參個數不同:

int add(int x,int y);

int add(int x,int y,int z);
例題:編寫兩個名爲sumOfSquare的重載函數,分別求兩個整數的平方和以及連個實數的平方和
#include"iostream"
using namespace std;
int sumOfSquare(int a, int b) {
	return a * a + b * b;
}
double sumOfSquare(double a, double b) {
	return a * a + b * b;
}
int main()
{
	int m, n;
	cout << "Enter two integer:";
	cin >> m >> n;
	cout << "Their sum of square:" << sumOfSquare(m, n) << endl;
	double x, y;
	cout << "Enter two integer:";
	cin >> x >> y;
	cout << "Their sum of square:" << sumOfSquare(x, y) << endl;
	return 0;
}
運行結果:
Enter two integer:5 6
Their sum of square:61
Enter two integer:3.2 2.3
Their sum of square:15.53

5.c++系統函數

C++的系統庫中提供了幾百個函數可供程序員使用,例如:

求平方根函數(sprt)

求絕對值函數(abs)

使用系統函數是要包含相應的頭文件,例如:

cmath
eger:5 6
Their sum of square:61
Enter two integer:3.2 2.3
Their sum of square:15.53

## 5.c++系統函數

**C++的系統庫中提供了幾百個函數可供程序員使用,例如:**

求平方根函數(sprt)

求絕對值函數(abs)

**使用系統函數是要包含相應的頭文件,例如:**

cmath



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章