第九章学习笔记

   第九章
1. 关键字 return
2.         运算符   *  &
3. 函数及其定义的方式
4. 参数和返回值的使用方式
5. 使用指针变量作为函数参数
6. 函数类型
7. ANSI C类型
8. 递归
//


1. 递归
   1)  每一级的函数调用都有自己的变量,也就是说第一级的变量n并非是第二级的变量。
int main(void)
{
    up_and_down(1);
    return 0;
}


void up_and_down(int n)
{
    printf("Level %d: n location %p\n", n, &n); /* 1 */
    if (n < 4)
        up_and_down(n+1);
    printf("LEVEL %d: n location %p\n", n, &n); /* 2 */
    
}
Level 1: n location 0x7fff5fbff8ec
Level 2: n location 0x7fff5fbff8cc
Level 3: n location 0x7fff5fbff8ac
Level 4: n location 0x7fff5fbff88c
LEVEL 4: n location 0x7fff5fbff88c
LEVEL 3: n location 0x7fff5fbff8ac
LEVEL 2: n location 0x7fff5fbff8cc
LEVEL 1: n location 0x7fff5fbff8ec

2)每一次的函数调用都会有一次返回。下一级执行到结束之后返回到上一级的函数调用处。
3)递归函数中,位于递归调用前的语句和各级调用函数都具有相同的顺序。
4)递归函数中,位于递归调用后的语句的执行顺序和各个被调用函数的顺序方相反。
5)虽然每一级递归都有自己的变量,但是函数代码并不会得到复制。
6)递归中必须包含可以终止递归调用的语句存在,否则将出现死循环。


2 .尾递归。递归调用出现在函数的尾部,即在return语句之前。
   //使用 循环进行阶乘
int factor(int i)
{
    int result=1,j;
    for(j = 1 ;j <= i; j++)
   {
        result *= j;
    }
    
    return result;
   
}


//使用递归进行阶乘操作


int rfactor(int num)
{
    int result;
    
    if(num > 0)  //递归的回归语句
        result = num * rfactor(num-1);
    else
        result = 1;
    
    return result;
    
}
//下面便是一个尾递归的例子。
int main(int argc, const char * argv[])
{
    printf("please input the number:");
    int num ;
    scanf("%d",&num);
    printf("%d \n",factor(num));
    printf("%d \n",rfactor(num));
    return 0;
}




3.递归和反向计算。
  void Conver(int num)
{
    int r = num % 2;
    if(num >= 2)
        Conver(num / 2);
    
    printf("%d",r);
}


4. 使用递归的优缺点。
   斐波那契数列。
      1 ,1,2,3,5,8,13….


long Finonacci(int n)
{
    if (n > 2)
        return Finonacci(n - 1) + Finonacci(n - 2);
    else
        return 1;    
}


5. 多源代码文件程序的编译
1) UNIX  
I.  cc file.c file2.c
II. cc file.o file2.o
2) linux
    I   gcc file.c file2.c
   II   gcc  file.o file2.o


6.头文件的使用
   自定义头文件然后调用


7.%*s的用法。



















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值