zoukankan      html  css  js  c++  java
  • 【C语言学习】《C Primer Plus》第9章 函数

    学习总结

     

    1、函数有利于我们可以省去重复的代码,函数可以使程序更加模块化,从而有利于程序的阅读、修改和完善。我们在系统设计或架构设计的时候,往往追求的是模块化、组件化、松耦合,而函数就是其代码的表现。许多程序员喜欢把函数看作“黑盒子”,即对应一定的输入产生特定的结果或返回某个数值,而黑盒子的内部行为并不需要考虑,然而有助于把精力投入到程序整体设计而不是其实现细节。按照C设计原则,我们不应为每个任务编写一个单独的函数,而应该尽量把函数的功能进行抽象设计到达通用目的。

     

    2、函数的组成部分有函数类型、函数名、函数参数(形参)、函数体实现、函数返回(视函数类型而定)。如下图所示:

    3、在ANSI C规范之前的传统的函数声明形式是不够准确的,因为它只声明了函数的返回值类型,而没有声明其参数。如int min();这个是ANSI C之前形式的声明通知编译器min()返回一个int类型的数值。然而,该语句并没有说明min()的参数个数和类型。因此,如果在函数min()中使用错误的参数类型和参数个数不对,编译器就不能发现这种错误。

     

    4、一个函数调用其本身的过程被称为递归。递归可以代替循环,反之亦然。递归其优点在于为某些编程问题提供了最简单的解决方法,而缺点是一些递归算法会很快耗尽计算机的内存资源。同时,使用递归的程序难于阅读和维护。

     

    5、与指针相关的运算符:&、*,这两个都是一元运算符。运算符后面跟随一个变量,如&变量为给出该变量的地址,而这个指针地址在大多数系统内部,它是由一个无符号整数表示,但并非可以把指针看作是整数类型,一些处理整数的方法不能用来处理指针。*运算符是用来获取存储在被指向地址中的数值。*指针变量就是获取该存放在该指针变量中的值。

     

    6、指针也是有类型的,什么样的数据类型就需要什么样的类型指针,如整数类型指针定义:

    int *pi;

    这里的星号(*)表示该变量为一指针。Pi是一个指针,而*pi是一个int类型的指针。

     

    7、函数往往可以通过指针参数来改变外部的变量,如:

     1 #include <stdio.h>
     2 void changeValue(int *a,int *b);
     3 int main(){
     4         int a=1,b=2;
     5         changeValue(&a,&b);
     6         printf("a=%d,b=%d
    ",a,b);
     7         return 0;
     8 }
     9 void changeValue(int *a,int *b){
    10         int temp;
    11         temp = *a;
    12         *a=*b;
    13         *b=temp;
    14 }

    打印结果:

    a=2,b=1

     

    8、编程题(题6)

     1 #include <stdio.h>
     2 
     3 double power(double n,int p);
     4 
     5 int main(){
     6         double x,xpow;
     7         int exp;
     8 
     9         printf("Enter a number and the positive integer power to which
    ");
    10         printf("the number will be reduced.Enter q to quit.
    ");
    11 
    12         while(scanf("%lf%d",&x,&exp)==2){
    13                 xpow=power(x,exp);
    14                 printf("%.3g to the power %d is %.5g
    ",x,exp,xpow);
    15                 printf("Enter next pair of numbers or q to quit.
    ");
    16         }
    17 
    18         printf("Hope you enjoyed this power trip --byte!
    ");
    19         return 0;
    20 }
    21 
    22 double power(double n,int p){
    23         double pow=1;
    24         int i;
    25         if(n==0)
    26                 return 0;
    27         if(n==1)
    28                 return 1;
    29         for(i=1;i<=p;i++){
    30                 pow*=n;
    31         }
    32         return 1/pow;
    33 }

    运行结果:

    Enter a number and the positive integer power to which

    the number will be reduced.Enter q to quit.

    2

    3

    2 to the power 3 is 0.125

    Enter next pair of numbers or q to quit.

    4

    5

    4 to the power 5 is 0.00097656

    Enter next pair of numbers or q to quit.

    1

    3

    1 to the power 3 is 1

    Enter next pair of numbers or q to quit.

    1

    5

    1 to the power 5 is 1

    Enter next pair of numbers or q to quit.

    0

    3

    0 to the power 3 is 0

    Enter next pair of numbers or q to quit.

    0

    81

    0 to the power 81 is 0

    Enter next pair of numbers or q to quit.

    q

    Hope you enjoyed this power trip --byte!

    9、编程题(题7)

     1 #include <stdio.h>
     2 
     3 double power(double n,int p,double pow);
     4 
     5 int main(){
     6         double x,xpow;
     7         int exp;
     8 
     9         printf("Enter a number and the positive integer power to which
    ");
    10         printf("the number will be reduced.Enter q to quit.
    ");
    11 
    12         while(scanf("%lf%d",&x,&exp)==2){
    13                 xpow=power(x,exp,1);
    14                 printf("%.3g to the power %d is %.5g
    ",x,exp,xpow);
    15                 printf("Enter next pair of numbers or q to quit.
    ");
    16         }
    17 
    18         printf("Hope you enjoyed this power trip --byte!
    ");
    19         return 0;
    20 }
    21 
    22 
    23 double power(double n,int p,double pow){
    24         if(n==0)
    25                 return 0;
    26         if(n==1)
    27                 return 1;
    28         if(p==0)
    29                 return n;
    30         if(p==1)
    31                 return 1/(n*pow);
    32         return power(n,--p,pow*n);
    33 }
  • 相关阅读:
    oracle 数据库 Cause: java.sql.SQLSyntaxErrorException: ORA-00904: "BODY": 标识符无效
    windows获取所有连接过的无线网密码
    element-ui 组件 el-calendar 农历显示问题
    mysql表复制
    console 打印消息时,可以使用 %c 指定随后的文本样式; %s 可引用参数变量。
    mysql 查询出现 "this is incompatible with sql_mode=only_full_group_by"错误解决方案,以及个人rpm方式重装所遇到的问题备份
    java 关于xlsx(xls) 和 csv 文件的数据解析
    idea websorm 激活码(2020-1-6 实测可用)最新
    mysql 连接查询 转换group_concat, find_in_set
    今天带来compass的使用方式
  • 原文地址:https://www.cnblogs.com/wcd144140/p/4584076.html
Copyright © 2011-2022 走看看