无论传递什么参数,函数都有副本机制

改变一个变量,需要传入变量的地址

改变一个指针变量,需要传入指针变量的地址

//int add(int a, int b);挖取函数声明

//int ()(int a, int b);换成括号
//int (*p)(int a, int b);加上*指针名

 #define _CRT_SECURE_NO_WARNINGS

 #include<stdio.h>
#include<stdlib.h> int add(int a, int b)
{
return a + b;
} main()
{
//int add(int a, int b);挖取函数声明
//int ()(int a, int b);换成括号
//int (*p)(int a, int b);加上*指针名 int(*p)(int a, int b) = add;
printf("%d", p(, )); system("pause");
}

例11.2

通过给 trans 函数传送不同的函数名,求 tan x 和 cot x 值。

 #include <stdio.h>
#include <math.h>
double tran(double(*) (double), double(*) (double), double); /* 函数说明语句 */
main()
{
double y, v;
v = * 3.1416 / 180.0;
y = tran(sin, cos, v); /* 第一次调用 */
printf("tan(60)=%10.6f\n", y);
y = tran(cos, sin, v); /* 第二次调用 */
printf("cot(60)=%10.6f\n", y);
}
double tran(double(*f1) (double), double(*f2) (double), double x)
{
return (*f1) (x) / (*f2)(x);
}

例11.3

用递归的方法求n!

求n!可用以下数学关系表示:

n!= 1           当n=0时

n!= n * ( n - 1 )!  当n>0时

 #include <stdio.h>
int fac(int n)
{
int t;
if (n == || n == )
{
return ;
}
else
{
t = n*fac(n - );
return t;
}
}
main()
{
int m, y;
printf("Enter m:");
scanf("%d", &m);
if (m < )
{
printf("Input data error !\n");
}
else
{
y = fac(m);
printf("\n%d!=%d\n", m, y);
}
}

例11.4

用递归算法根据以下求平方根的迭代公式求某数 a 的平方根:

x1 = (x0 + a / x0) / 2
 #include <stdio.h>
#include <math.h>
double mysqrt(double a, double x0)
{
double x1;
x1 = (x0 + a / x0) / 2.0;
if (fabs(x1 - x0) > 0.00001)
{
return mysqrt(a, x1);
}
else
{
return x1;
}
}
main()
{
double a;
printf("Enter a:");
scanf("%lf", &a);
printf("The sqrt of %f=%f\n", a, mysqrt(a, 1.0));
}

11.12

请编写递归函数,把输入的一个整数转换成二进制数输出。

 #include <stdio.h>
void outninary(int a)
{
int d;
d = a % ;
if (a != )
{
outninary(a / );
printf("%d", d);
}
}
main()
{
int a;
scanf("%d", &a);
outninary(a);
}

11.13

请用递归算法,求 1+2+3+...+n,n 由键盘输入。

 #include <stdio.h>
int sum(int n)
{
if (n != )
{
return n + sum(n - );
}
else
{
return ;
}
}
main()
{
int n, y;
scanf("%d", &n);
y = sum(n);
printf("y=%d", y);
}

11.14

请用递归算法,求数列 Fibonacci。求 n 阶 Fibonacci 数列的公式如下:

1         当 n=0 时

F(n)= 1         当 n=1 时

F(n-1)+F(n-2)  当 n>1 时

 #include <stdio.h>
int f(int n)
{
if (n == || n == )
{
return ;
}
else
{
return (f(n - ) + f(n - ));
}
}
main()
{
int n, y;
scanf("%d", &n);
y = f(n);
printf("y=%d", y);
}

全国计算机等级考试二级教程-C语言程序设计_第11章_对函数的进一步讨论的更多相关文章

  1. 全国计算机等级考试二级教程-C语言程序设计_第4章_选择结构

    switch什么时候用break,什么时候不用break 调用break:一次执行一个分支,输入一个数据,对应一个级别 不调用break:连续执行多个分支 if...else 可以处理任何情况,大于小 ...

  2. 全国计算机等级考试二级教程-C语言程序设计_第8章_地址和指针

    面试: unsigned int *p1 = &num; int *p2 = &num; #define _CRT_SECURE_NO_WARNINGS #include<std ...

  3. 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算

    位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...

  4. 全国计算机等级考试二级教程-C语言程序设计_第9章_数组

    四维数组,可用于航天卫星,三维+时间 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  5. 全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型

    函数的返回值是结构体类型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

    字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...

  7. 全国计算机等级考试二级教程-C语言程序设计_第5章_循环结构

    for循环结构的嵌套 外层循环每循环一次,内层循环会完整循环一次. 外层循环是竖. 内层循环是横. for, do...while, while的选择: 如果有固定次数,如阶乘! ,判断素数,用 fo ...

  8. 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构

    1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...

  9. 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  10. 全国计算机等级考试二级教程-C语言程序设计_第7章_函数

    函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...

随机推荐

  1. KeyEvent

    http://blog.csdn.net/elfylin/article/details/8008763 一. 接口KeyEvent.Callback和View.OnKeyListener 二. 流程 ...

  2. registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. (转)

    最近项目中遇见一问题,在开发环境没有问题的代码,到了生产环境就会报如下错误:   严重: A web application registered the JBDC driver [oracle.jd ...

  3. UDP包的大小与MTU

    在进行UDP编程的时候,我们最容易想到的问题就是,一次发送多少bytes好?当然,这个没有唯一答案,相对于不同的系统,不同的要求,其得到的答案是不一样的,我这里仅对像ICQ一类的发送聊天消息的情况作分 ...

  4. accel-pptp 部署

    accel-pptp 是 pptp-client 和 pptpd 的改进版,使用内核 pptp 模块,相比 raw socket 实现方式能提供更好的性能.   Ubuntu 12.04 上启用内核 ...

  5. java中如何将char数组转化为String

    1.直接在构造String时建立. char data[] = {'s', 'g', 'k'}; String str = new String(data); 2.String有方法可以直接转换. S ...

  6. 代码管理git总结

    1. http://blog.csdn.net/teresa502/article/details/7388834 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 ...

  7. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  8. R-plot

    颜色.图例和线 在散点图中添加信息.图例以及回归线. 模拟数据 #模拟数据 dat <- data.frame(X = runif(100,-2,2),T1 = gl(n=4,k=25,labe ...

  9. About Health Monitor Checks

    About Health Monitor Checks Health Monitor checks (also known as checkers, health checks, or checks) ...

  10. 【线段树】【3-21个人赛】【同样的problemB】

    同一道题  http://blog.csdn.net/zy691357966/article/details/44680121 区间查询最大值 用线段树 比单调队列慢 #include <cst ...