第七章 c++的編程模塊

複習函數是如何工作的

着重介紹使用函數如何處理數組/字符串/結構

介紹遞歸和函數指針

複習函數的基本知識

  • 提供函數定義
  • 提供函數原型
  • 調用函數

calling.cpp

#include <iostream>

void simple();  //defining

int main()
{
	using namespace std;
	cout << "main() will call the simple() function:\n";
	simple();   //function call
	cout << "main() is finished with the simple() function.\n";
	cin.get();
	return 0;
}
void simple()    //prototyping
{
	using namespace std;
	cout << "I'm but a simple function.\n";
}

可以將函數分爲兩類:沒有返回值的函數(void函數)和有返回值的函數

void function(parameterlist)   //parameterlist:形參
{
	statement(s)
	return;
}

** while循環和for循環**

#include <iostream>

void simple(int);

int main()
{
    using namespace std;
    cout << "main() will call the simple() function:\n";
    simple(0);   //function call
    cout << "main() is finished with the simple() function.\n";
    cin.get();
    return 0;
}
void simple(int n)
{
    using namespace std;
    cout << n << endl;
//    while(n<=10)
//    {
//        cout << "I'm but a simple function." << n << endl;
//        ++n;
//        if (n==8){break;}  //循環
//    }
    for(n=0;n<=10;n++)
    {
        cout << "I'm but a simple function." << n << endl;
        //++n;
        if (n==9){break;}  //循環
        //使用for循環,最終n輸出爲最大值
    }
    cout << n << endl;
}

有返回值的函數

typeName functionName(parameterList)  //函數不能返回數組,但是可以將數組作爲結構或者對象組成部分返回
{
	statements;
	return value;
}

protos

#include <iostream>

void cheers(int);
double cube(double x);//函數原型中可以包含變量名,也可以不包括


int main()
{
    using namespace std;
    cheers(5);
    cout << "Give me a number: ";
    double side;
    cin >> side;
    double volume = cube(side);
    cout << "A" << side << "-foot cube has a volume of ";
    cout << volume << " cubic feet.\n";
    cheers(int(cube(2)));
    return 0;
}

void cheers(int n)
{
    using namespace std;
    do
    {
        cout << "Cheers\n";
        n--;
    }while(n>0);
}

double cube(double x)
{
    return x * x * x;
}

處理數組的函數

int sur_arr(int arr[],int n);//sizeof(arr)

int sur_arr(int * arr,int n);//sizeof(arr)
兩種方式均正確,區別arr[]中,arr不僅指向int,還指向int數組的第一個值,而*arr指向一個獨立的值
注:1.調用數組時,數組用數組名

arr[i] == *(ar +i)    //ar+1數組加一個地址長度
&arr[i] == ar+i       //

區間數組

# include <iostream>

int arr_range(const int * begin,const int * end); //區間數組求和函數

int main()
{
	using namespace std;
	int arr_int[10] = {0,1,2,3,4,5,6,7,8,9};
	int i = 0;
	while(i < 10)
	{	
		cout << arr_int[i] << endl;
		i++;
		}
	//cin.get();
	int sum = arr_range(arr_int+8,arr_int+9);
	cout << sum << endl;
	return 0;
}


int arr_range(const int * begin,const int * end)
{
	const int * pt;
	pt = begin;
	int total = 0;
	while(pt!=end)
	{
		total = total + *pt;
		pt++;
	}
	return total;
}

指針和const

//此情況不能使用pm修改age的值
int age = 39;
const int * pm = &age;
//此情況不能修改pm的值,允許使用pm修改age的值
int age = 39;
int const  * pm = &age;
//可以使用const對象聲明const的指針
int age = 39;
const int const * pm = &age;

函數和二維數組

# include <iostream>
int main()
{
	using namespace std;
	int arr_Two_dimensional[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11,12}
	cout << arr_Two_dimensional[2][3]  << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章