函数设计

函数设计

函数是 C++/C 程序的基本功能单元,其重要性不言而喻。

函数设计的细微缺点很容 易导致该函数被错用,所以光使函数的功能正确是不够的。

本章重点论述函数的接口设 计和内部实现的一些规则。

函数接口的两个要素是参数和返回值。

C 语言中,函数的参数和返回值的传递方式 有两种:值传递(pass by value)和指针传递(pass by pointer)。C++ 语言中多了引用传 递(pass by reference)。由于引用传递的性质象指针传递,而使用方式却象值传递,初 学者常常迷惑不解,容易引起混乱,引用与指针的比较”。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 float x=365.5;  //声明全局变量
 5 using namespace std;
 6 int main(int argc, char** argv) {
 7     int x=1,y=2;
 8     double w=x+y;
 9     {
10         double x=1.414,y=1.732,z=3.14;
11         cout<<"inner:x="<<x<<endl;
12         cout<<"inner:y="<<y<<endl;
13         cout<<"inner:z="<<z<<endl;
14         cout<<"outer:w="<<w<<endl;
15         cout<<"::x="<<::x<<endl;    //访问重名的全局变量
16     }
17     cout<<"outer:x="<<x<<endl;
18     cout<<"outer:y="<<y<<endl;
19     cout<<"outer:w="<<w<<endl;
20 
21     //cout<<"inner:z="<<z<<endl;无效
22     cout<<"::x="<<::x<<endl;    //访问重名的全局变量
23     return 0;
24 }
原文地址:https://www.cnblogs.com/borter/p/9406318.html