> For the complete documentation index, see [llms.txt](https://peiyafei.gitbook.io/c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://peiyafei.gitbook.io/c/di-er-zhang-he-xin-bian-cheng/10-yin-yong/10.4-yin-yong-zuo-han-shu-fan-hui-zhi.md).

# 10.4 引用做函数返回值

**作用：**&#x5F15;用是可以作为函数的返回值存在的

**注意：不要返回局部变量引用**

**用法：**&#x51FD;数调用作为左值

**示例：**

```
//返回局部变量引用
int& test01() {
	int a = 10; //局部变量
	return a;
}

//返回静态变量引用
int& test02() {
	static int a = 20;
	return a;
}

int main() {

	//不能返回局部变量的引用
	int& ref = test01();
	cout << "ref = " << ref << endl;
	cout << "ref = " << ref << endl;

	//如果函数做左值，那么必须返回引用
	int& ref2 = test02();
	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	test02() = 1000;

	cout << "ref2 = " << ref2 << endl;
	cout << "ref2 = " << ref2 << endl;

	system("pause");

	return 0;
}
```
