学习总结

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

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

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

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

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

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

int *pi;

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

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

 #include <stdio.h>
void changeValue(int *a,int *b);
int main(){
int a=,b=;
changeValue(&a,&b);
printf("a=%d,b=%d\n",a,b);
return ;
}
void changeValue(int *a,int *b){
int temp;
temp = *a;
*a=*b;
*b=temp;
}

打印结果:

a=2,b=1

8、编程题(题6)

 #include <stdio.h>

 double power(double n,int p);

 int main(){
double x,xpow;
int exp; printf("Enter a number and the positive integer power to which\n");
printf("the number will be reduced.Enter q to quit.\n"); while(scanf("%lf%d",&x,&exp)==){
xpow=power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
} printf("Hope you enjoyed this power trip --byte!\n");
return ;
} double power(double n,int p){
double pow=;
int i;
if(n==)
return ;
if(n==)
return ;
for(i=;i<=p;i++){
pow*=n;
}
return /pow;
}

运行结果:

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)

 #include <stdio.h>

 double power(double n,int p,double pow);

 int main(){
double x,xpow;
int exp; printf("Enter a number and the positive integer power to which\n");
printf("the number will be reduced.Enter q to quit.\n"); while(scanf("%lf%d",&x,&exp)==){
xpow=power(x,exp,);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
} printf("Hope you enjoyed this power trip --byte!\n");
return ;
} double power(double n,int p,double pow){
if(n==)
return ;
if(n==)
return ;
if(p==)
return n;
if(p==)
return /(n*pow);
return power(n,--p,pow*n);
}

【C语言学习】《C Primer Plus》第9章 函数的更多相关文章

  1. C语言学习笔记:14_内部函数和外部函数

    /* * 14_内部函数和外部函数.c * * Created on: 2015年7月5日 * Author: zhong */ #include <stdio.h> #include & ...

  2. C语言学习之我见-memchr()内存查找字符函数

    memchr()内存查找字符函数:主要用于从内存中查找自己需要的字符位置. (1)函数原型: void *memchr(const void *_Buf ,int _Val,size_t _MaxCo ...

  3. C语言学习笔记 (005) - 二维数组作为函数参数传递剖析

    前言 很多文章不外乎告诉你下面这几种标准的形式,你如果按照它们来用,准没错: //对于一个2行13列int元素的二维数组 //函数f的形参形式 f(int daytab[2][13]) {...} / ...

  4. R语言学习笔记(七): 排序函数:sort(), rank(), order()

    sort() sort()函数直接对函数进行排序,并返回排序结果. > a <- c(12,4,6,5) > sort(a) [1] 4 5 6 12 rank() rank()函数 ...

  5. C++ Primer 5th 第6章 函数

    正如第一章所说:C++的函数是一个能够完成一个功能的模块或者说是一段命名了的代码块. 如下图所示,函数可以重载,是一段实现某些功能命名了的代码. 一个完整的函数的构成有四部分: 1.返回类型 2.函数 ...

  6. C Primer Plus 第9章 函数 编程练习

    复习题: 8. int choice(int a,int b,int c){ int max; max = a; if (b > max) max = b; if (c > max) ma ...

  7. Go语言学习笔记四: 运算符

    Go语言学习笔记四: 运算符 这章知识好无聊呀,本来想跨过去,但没准有初学者要学,还是写写吧. 运算符种类 与你预期的一样,Go的特点就是啥都有,爱用哪个用哪个,所以市面上的运算符基本都有. 算术运算 ...

  8. C语言学习书籍推荐《C Primer Plus(中文版)(第5版)》下载

    普拉塔 (Prata S.) (作者), 云巅工作室 (译者) <C Primer Plus(中文版)(第5版)>共17章,介绍了C语言的基础知识,包括数据类型.格式化输入输出.运算符.表 ...

  9. 【C语言学习】《C Primer Plus》第1章 概览

    学习总结 1.C语言于1972年由贝尔实验室的Dennis Ritchie在与Ken Thompson一起设计UNIX操作系统的时候开发的.的的设计构想来源于Ken Thompson的B语言.Anyw ...

  10. linux 下C语言学习路线

    UNIX/Linux下C语言的学习路线.一.工具篇“公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工 ...

随机推荐

  1. ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )

    <input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...

  2. ios6 滤镜相关知识内容网址---摘要

    http://blog.csdn.net/justinjing0612/article/details/8145607#

  3. error while performing database login with the xxx driver

    在MyEclipse的安装路径下D:\Program Files\MyEclipse 6.0\eclipse下面找到eclipse.ini文件,用记事本打开 eclipse.ini文件 -showsp ...

  4. 13.Xcode开发的快捷键

    1.文件 CMD + N: 新文件: CMD + SHIFT + N: 新项目: CMD + O: 打开: CMD + S: 保存: CMD + SHIFT + S: 另存为: CMD + W: 关闭 ...

  5. js中创建数组的方法

    1.声明或创建一个不指定长度的数组(Array)的方式为: 如:var arrayObj = new Array(); 2.声明或创建一个数组并指定长度的数组(Array)的方式为: 如:var ar ...

  6. redis迁移工具-redis-migrate-tool使用测试

    https://github.com/vipshop/redis-migrate-tool一.安装redis-migrate-tool a.下载redis-migrate-tool软件包 https: ...

  7. Debian 8下vsftpd安装与配置

    Debian 8下vsftpd安装与配置 0.环境 root@remote:/# uname -r 3.16.0-4-amd64 root@remote:/e# lsb_release No LSB ...

  8. Java反射机制及IoC原理

    一. 反射机制概念 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义.在java中,只要给定类的名字, 那么就可以通 ...

  9. MATLAB 画出三个通信小区cell边界示意图

    d=1000; %两个小区中心间距离的一半 rcell=2*d/sqrt(3); %小区半径 ncell=3; %小区个数 cellposition=zeros(ncell,2); %初始化小区中心位 ...

  10. Emgu CV播放视频

    public partial class Form1 : Form {     Capture _capture;     public Form1()     {         Initializ ...