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

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

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

//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. win7 安装 redis +php扩展

    1:首先下载redis:redis-2.0.2.zip (32 bit),解压. 从下面地址下:http://code.google.com/p/servicestack/wiki/RedisWind ...

  2. Apriori algorithm

    本文是个人对spmf中example1. mining frequent itemsets by  using the apriori algorithm的学习. What is Apriori? A ...

  3. [python 基础] Class 一些基本概念

    class example(object): data1 = '' date2 = "" def __init__(self, para): self._function1() d ...

  4. 重写 libev 的 EV_WIN32_HANDLE_TO_FD

    libev 的 EV_WIN32_HANDLE_TO_FD 默认实现是调用C库的  _open_osfhandle ,但这里有个问题是转换后,关闭 fd 就默认关闭了 handle.当它遇到 libc ...

  5. 解决GitHub未配置SSH key提示错误信息

    git push -u origin master Permission denied (publickey). fatal: Could not read from remote repositor ...

  6. php float 转int

    round(x,prec) 参数 描述 x 可选.规定要舍入的数字. prec 可选.规定小数点后的位数. <?php echo(round(0.60)); echo(round(0.50)); ...

  7. hdu 5506 GT and set(dfs爆搜)

    Problem Description You are given N sets.The i−th set has Ai numbers.You should divide the sets into ...

  8. OC基础12:数字、字符串和集合1

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.有时要将一些数字数据类型的值当做对象来 ...

  9. 【网络协议】TCP的拥塞控制机制

    前言 计算机网络中的带宽.交换节点中的缓存和处理机等,都是网络的资源,在某段时间内,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏,这样的情况就叫做拥塞. 所谓拥塞控制,就 ...

  10. PHP定义数组常量

    最先想到的方法是这样: define('SIGN_CODE', array('9df512','59gf1g','5eg7h1','g1agf5','f5e151','g51gfr','a5481s' ...