一行代码只做一件事情

一行代码只做一件事情,如只定义一个变量,或只写一条语句。这样 的代码容易阅读,并且方便于写注释。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6         //函数原型声明
 7     int fact(int x);
 8     int n,sn;
 9 
10     //依次从键盘上输入3个正整型数据计算它们的阶乘
11     for (int i=1;i<=3;i++)
12     {
13         cout<<i<<"   n=";
14         cin>>n;
15         sn=fact(n);
16         cout<<n<<"!="<<sn<<endl;
17     }
18     return 0;
19 }
20 
21 
22 //以下是采用递归方法定义的fact()函数
23 int fact(int x)
24 {
25    if (x==0) return(1);
26       return(x*fact(x-1));  //此处又调用了它自身
27 }