C++入门学习七 定义函数

第八章 定义函数

定义函数

返回类型+函数名+参数列表(可选)

void 无返回值

#include <iostream>
using namespace std;

int add(int a, int b) {
	return a + b;
}

int main()
{
	cout << add(1, 2) << endl;
	return 0;
} 

函数原型(函数声明)

函数的定义也是函数声明

必须把函数声明放在函数调用之前

按值传送

指定为实参的变量值或常量值不会传递给函数,而是创建实参副本,把这些副本传递给函数

#include <iostream>
using namespace std;

int add(int a, int b) {
	return a + b;
}

int main()
{
	int a{ 1 };
	cout << add(a, 2) << endl;
	cout << a << endl; // a的值还是1,传给add()函数的只是a实参的副本
	return 0;
} 

给函数传送指针

#include <iostream>
using namespace std;

int add(int* a, int* b) {
	return *a + *b;
}

int main()
{
	int a{ 1 };
	int b{ 2 };
	cout << add(&a, &b) << endl;
	return 0;
} 

给函数传送数组

数组名本质是地址

#include <iostream>
using namespace std;

void fun1(int arr[],size_t s) {
	for (size_t i = 0; i < s; i++) {
		arr[i] += 1;
	}
}

int main()
{
	int a[5]{ 1,2,3,4,5 };
	fun1(a, size(a));
	for (size_t i = 0; i < size(a); i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;
} 

按引用传送

#include <iostream>
using namespace std;

void fun1(int &a) {
	a = a + 1;
}

void fun2(int a) {
	a = a + 1;
}

int main()
{
	int a{ 0 };
	int b{ 0 };
	fun1(a);
	fun2(b);
	cout << a << endl; // 1
	cout << b << endl; // 0
	return 0;
} 

引用和指针传送类似,但是指针允许nullptr,引用不能为null

main函数的实参

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	// argc 表示命令行输入字符串实参的个数,argv是指针数组,指向命令实参,argv[]中最后一个元素总是nullptr
	return 0;
} 

使用可选值std::optional<T>

作用:处理返回值为空或者无效值的情况

optional<T>.value():返回该optional<T>的值

optional<T>.has_value():判断optional<T>是否是无效值

optional<T>.value_or(arg):判断optional<T>是否是无效值,如果有效返回该值,无效返回arg的值

std::nullopt:用于初始化还没有给T赋值的optional<T>值

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

void infor_human(string name, optional<int> high = nullopt, optional<int> age = nullopt, optional<string> class_name = nullopt) {
	cout << "name: " << name << " ";
	if (high.has_value()) {
		cout << "high: " << high.value() << " ";
	}
	if (age.has_value()) {
		cout << "age:" << age.value() << " ";
	}
	cout << "class_name:" << class_name.value_or("civilian") << " ";
	cout << endl;
}

int main()
{
	infor_human("xiaoming");
	infor_human("xiaohong", 170, 16);
	infor_human("xiaozhang", 180, 16, "soldier");
} 

静态变量

第一次执行函数时创建,后面不再重复重复创建,用于函数多次调用,传递该值

#include <iostream>
using namespace std;

void fun1() {
	static int count{ 0 };
	count++;
	cout << count << endl;
}

int main()
{
	fun1(); // 1
	fun1(); // 2
	fun1(); // 3
} 

内联函数inline

编译器直接使用函数体实际代码替代

#include <iostream>
using namespace std;

inline int fun1(int a, int b) {
	return a + b;
}

int main()
{
	fun1(1, 2);
} 

函数重载

函数名相同,参数列表不同

#include <iostream>
using namespace std;

int fun1(int a, int b) {
	cout << 1 << endl;
	return a + b;
}

double fun1(double a, double b) {
	cout << 2 << endl;
	return a + b;
}

int main()
{
	cout << fun1(1, 2) << endl;
	cout << fun1(1.1, 2.2) << endl;
} 

指向不同类型指针是不同的

#include <iostream>
using namespace std;

int fun1(int* a, int b);
int fun1(double* a, int b);

int main() {
	return 0;
}

重载和引用

注意:不能把参数类型为type的重载为type&的函数,编译器无法区分。

一个版本时type1,另一个版本时type2的引用,调用哪个取决于实参类型

#include <iostream>
using namespace std;

int fun1(int a, int b) {
	cout << 1 << endl;
	return a + b;
}
double fun1(double& a, double& b) {
	cout << 2 << endl;
	return a + b;
}


int main() {
	int a{ 1 };
	int b{ 2 };
	float c{ 1.0 };
	float d{ 2.0 };
	fun1(a, b); // 1
	fun1(static_cast<double>(c), static_cast<double>(d)); // 1,不会调用第二个
	return 0;
}

可以把参数指定为const引用来避免这个问题

#include <iostream>
using namespace std;

int fun1(int a, int b) {
	cout << 1 << endl;
	return a + b;
}
double fun1(double& a, double& b) {
	cout << 2 << endl;
	return a + b;
}
double fun1(const double& a, const double& b) {
	cout << 3 << endl;
	return a + b;
}

int main() {
	int a{ 1 };
	int b{ 2 };
	float c{ 1.0 };
	float d{ 2.0 };
	fun1(a, b); // 1
	fun1(static_cast<double>(c), static_cast<double>(d)); // 3
	return 0;
}

一般不要为重载函数指定默认参数,不然,在没有参数的时候,编译器不知道调用哪个

递归

函数调用本身

#include <iostream>
using namespace std;

int fi(int n) {
	if (n == 1) {
		return 1;
	}
	return n + fi(n - 1);
}


int main() {
	int n;
	cin >> n;
	cout << fi(n);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值