第七章、函数

7.1函数简介

1、函数的定义

2、函数的调用

3、函数的作用域

4、参数

5、返回值

6、静态局部对象

7.2参数传递

1、按值传递

#include <iostream>
#include <string>
using namespace std;

// 按值传递
void printThings(int intNum, float floatNum, string str) 
{
	cout << "打印整数:" << intNum << endl;
	cout << "打印浮点数:" << floatNum << endl;
	cout << "打印字符串:" << str << endl;
}

int main() {
	int a = 3;
	float b = 1.0f;
	string str = "string";
	printThings(a, b, str);
	return 0;
}

2、指针传递

#include <iostream>
using namespace std;

// 按值传递实现swap
void swap(int a, int b) 
{
	int temp = a;
	a = b;
	b = temp;
	cout << "在交换函数末尾,a等于" << a << ",b等于" << b << endl;
}

int main() 
{
	int a = 3;
	int b = 4;
	cout << "交换前,a等于" << a << ",b等于" << b << endl;
	swap(a, b);
	cout << "交换后,a等于" << a << ",b等于" << b << endl;
	return 0;
}

3、引用传递

#include <iostream>
using namespace std;

// 引用传递实现swap
void swap(int &a, int &b) {
	int temp = a;
	a = b;
	b = temp;
	cout << "在交换函数末尾,a等于" << a << ",b等于" << b << endl;
}

int main() {
	int a = 3;
	int b = 4;
	cout << "交换前,a等于" << a << ",b等于" << b << endl;
	swap(a, b);
	cout << "交换后,a等于" << a << ",b等于" << b << endl;
	return 0;
}

4、const参数

#include <iostream>
#include <string>
using namespace std;

// const引用参数
char getLastChar(const string &str) {
	// 把这行注释掉程序就能正常运行
	str[str.length() - 1] = 's';
	return str[str.length() - 1];
}

int main() {
	string str = "hello";
	cout << "字符串的最后一个字符为:" << getLastChar(str) << endl;
	return 0;
}

5、数组参数

#include <iostream>
using namespace std;

// 数组参数
void printArrAddr(int arrParam[5]) {
	cout << "arrParam的地址是:" << arrParam << endl;
}

int main() {
	int arr[5] = { 0, 1, 2, 3, 4 };
	cout << "arr的地址是:" << arr << endl;
	printArrAddr(arr);
	return 0;
}

6、main()函数的参数

#include <iostream>
using namespace std;

// main函数的参数
int main(int argc,char **argv) 
{
	int sum = 0;
	for ( int i = 0; i < argc; i++) 
	{
		// atoi函数将C风格字符串转换成数字
		sum += atoi(argv[i]);
	}
	cout << "输入所有数字的总和为:" << sum << endl;
	return 0;
}

7.3函数返回值

1、返回值或对象

返回值和按值传参一样都是赋值操作

#include <iostream>
using namespace std;

// 返回值的隐式转换
int mad(int a, int b, int c) {
	return a * b + c;
}

int main() {
	float fa = 3.5;
	float fb = mad(3, 4, 5);
	cout << "fb的值为:" << fb << endl;
	cout << "fa + fb = " << fa + fb << endl;
	return 0;
}

2、返回引用

返回局部对象的引用会引发错误

#include <iostream>
#include <string>
using namespace std;

// 返回局部对象的引用
string &retLocal() 
{
	string ret = "Hello";
	string &retRef = ret;
	return retRef;
}

int main() 
{
	cout << "retArg返回值是:" <<  retLocal() << endl;
	return 0;
}

3、返回指针

返回局部对象的指针也会引发错误。
一般情况下函数返回指针都是返回动态分配的内存指针。
因此在设计这样的函数时,我们需要配套设计一个释放资源的函数。并提醒开发人员成对地调用这两个函数

#include <iostream>
using namespace std;

// 返回指针
int *allocIntArr(int size) 
{
	int *ret = (int*)malloc(size * sizeof(int));
	return ret;
}

void releaseIntArr(int *intArr) 
{
	delete intArr;
}

int main() {
	int size = 5;
	int *intArr = allocIntArr(size);
	for ( int i = 0; i < size; i++ ) 
	{
		intArr[i] = i;
	}
	for ( int i = 0; i < size; i++ ) 
	{
		cout<< "打印数组第" << i << "个元素:" << intArr[i] << endl;
	}
	releaseIntArr(intArr);
	return 0;
}

4、main()函数的返回值

0表示程序状态正常

7.4函数声明

1、函数声明与函数定义

2、默认参数

#include <iostream>
using namespace std;

// 默认参数

void printDate(int day, int month = 12, int year = 2018) 
{
	cout << "今天的日期是:" << year << "年" << month << "月" << day << "日" << endl;
}
int main() 
{
	printDate(30, 11);
	printDate(24);
	return 0;
}

3、内联函数

inline关键字

#include <iostream>
using namespace std;

// 内联函数
inline int max(int a, int b) 
{
	return a > b ? a : b;
}
int main()
{
	int a = 5;
	int b = 2;
	cout << "a和b之中的最大值是:" << max(a, b) << endl;
	return 0;
}

7.5函数重载

1、函数重载发生在相同的作用域里
声明两个一样的函数,并使用不同的参数类型。
2、重载解析
函数调用的时候,编译器寻找与实参列表最匹配的重载版本的过程。

#include <iostream>
using namespace std;

// 函数重载的概念
bool isEqual(int a, int b) {
	return a == b;
}

bool isEqual(float a, float b) {
	return abs(a - b) < 0.00001; //随意指定一个极小值
}

int main() {
	int a = 3;
	int b = 3;
	float fa = 3.0f;
	float fb = 3.001f;
	cout << "两个整数";
	if ( !isEqual(a, b) ) {
		cout << "不";
	}
	cout << "相等" << endl;
	cout << "两个浮点数";
	if ( !isEqual(fa, fb) ) {
		cout << "不";
	}
	cout << "相等" << endl;
	return 0;
}

7.6函数指针

1、创建和初始化

2、函数指针的应用

3、函数指针作为参数

4、函数指针作为返回值

#include <iostream>
using namespace std;

// 函数指针作为返回值

int (*funcRetFp(int a, int b))(int, int) 
{
	int(*fp)( int, int ) = NULL;
	return fp;
}
typedef int(*binaryFp)(int, int);
binaryFp funcRetFpTypedef(int a, int b) 
{
	binaryFp fp = NULL;
	return fp;
}
int main() 
{
	cout << funcRetFp(1, 2) << " " << funcRetFpTypedef(1, 2) << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值