C++_primer_plus学习笔记 第7章 函数——C++的编程模块

本章内容包括:

  • 函数基本知识
  • 函数原型
  • 按值传递
  • 处理数组的函数
  • const指针参数
  • 处理文本字符串的函数
  • 处理结构的函数
  • 处理string对象的函数
  • 调用自身的函数(递归)
  • 指向函数的指针

7.1 函数基本知识

原型、调用、定义

7.1.1 定义函数

  • 无返回值return可选,有返回值return必须
  • 返回值类型不能直接为数组
  • 可以将数组作为结构或对象组成部分间接返回

7.1.2 函数原型和函数调用

  • 为什么需要原型? 事先打个招呼,不致于太陌生
  • 原型参数列表中,可以包括变量名,也可以不包括(饭店订餐问几人几点到,而不会问每个人姓名)

7.2 函数参数和按值传递

7.2.1 多个参数

  • 如果函数的两个参数类型相同,必须为每个参数指定类型
    void fifi(float a, float b)
    void fufu(float a, b)    //NOT acceptable
//程序清单7.3
//函数有两个参数
#include <iostream>

using namespace std;

void n_chars(char c, int n);

int main(void)
{
	char ch;
	int times;

	cout << "Enter a character: ";
	cin >> ch;
	while (ch != 'q')
	{
		cout << "Enter an integer: ";
		cin >> times;
		n_chars(ch, times);
		cout << "\nEnter another character or press the q-key to quit: ";
		cin >> ch;
	}
	cout << "The value of times is " << times << ".\n";
	return 0;
}

void n_chars(char c, int n)
{
	while (n-- > 0)
		cout << c;
}
  • ch是字符,字符与数字混合输入时,cin>>ch可以跳过换行符
  • ch是字符串时,字符串与数字混合输入,cin.get(ch),注意消耗换行符
    while(cin.get() != '\n')
        continue;

7.3 函数和数组

//程序清单7.5
//函数与一维数组
#include <iostream>

using namespace std;

const int ArSize = 8;

int sum_arr(int arr[], int n);

int main(void)
{
	int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
	int sum = sum_arr(cookies, ArSize);
	cout << "Total cookies eaten: " << sum << endl;
	return 0;
}

int sum_arr(int arr[], int n)
{
	int total = 0;
	for (int i = 0; i < n; i++)
		total += arr[i];
	return total;
}
  • C++将数组名解释为其第一个元素的地址:cookies == &cookies[0]
  • 该规则有一些例外。首先数组声明使用数组名标记存储位置;其次对数组名使用sizeof得到整个数组的长度;第三地址运算符&用于数组名时返回整个数组的地址
int sum_arr(int arr[], int n)    //数组表示法
int sum_arr(int * arr, int n)    //指针表示法
//int arr[] 标识起始位置
//int n 标识整个数组在内存中的地址范围

arr[i] == *(arr + i)    //arr指针加i,取出值就是数组第i个元素的值
&arr[i] == arr + i      //arr指针加i,就是数组第i个元素的地址

int sum = sum_arr(cookies, ArSize)        //返回整个数组的和
sum = sum_arr(cookies, 3)                 //数组前3个元素的和
sum = sum_arr(cookies + 4, 4)             //数组后4个元素的和

7.3.3 更多数组示例

//程序清单7.7
//数组函数与const
#include <iostream>

using namespace std;

const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);

int main(void)
{
	double properties[Max];
	int size = fill_array(properties, Max);
	show_array(properties, size);
	if (size > 0)
	{
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor))			//错误输入清理专用
		{
			cin.clear();                    //清除错误标识位
			while (cin.get() != '\n')
				continue;                   //消耗缓冲区所有内容
				cout << "Bad input; Please enter a number: ";
		}
		revalue(factor, properties, size);
		show_array(properties, size);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

int fill_array(double ar[], int limit)
{
	double temp;
	int i;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> temp;
		if (!cin)							//错误输入清理专用
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
			break;
		}
		else if (temp < 0)
			break;
		ar[i] = temp;
//      else
//          ar[i] 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值