C語(yǔ)言-基礎(chǔ)教程-C語(yǔ)言程序應(yīng)用舉例

字號(hào):


    這是一個(gè)遞歸函數(shù)調(diào)用的例子。程序中函數(shù)f o r w a r d _ a n d _ b a c k w a r d s ( )的功能是顯示一個(gè)字符串后反向顯示該字符串。
    [例4-17] 計(jì)算1~7的平方及平方和。
    #include
    # include
    void header(); / *函數(shù)聲明* /
    void square(int number);
    void ending();
    int sum; /* 全局變量* /
    m a i n ( )
    {
    int index;
    h e a d e r ( ) ; / *函數(shù)調(diào)用* /
    for (index = 1;index <= 7;i n d e x )
    s q u a r e ( i n d e x ) ;
    e n d i n g ( ) ; / *結(jié)束* /
    }
    void header()
    {
    sum = 0; /* 初始化變量"sum" */
    printf("This is the header for the square program\n;\n")
    }
    void square(int number)
    {
    int numsq;
    numsq = number * numbe;r
    sum = numsq;
    printf("The square of %d is %d\,nn"u m b e r ,nu m s q ) ;
    }
    void ending()
    {
    printf("\nThe sum of the squares is %d,\ns"u m ) ;
    }