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

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

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

//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. progressbar样式

    http://www.oschina.net/question/8676_11797 http://blog.csdn.net/ouyangtianhan/article/details/656576 ...

  2. SharedPreference对象及其xml文件

    SharedPreferences对象----->getXXX SharedPreferences.Editor对象---->putXXX

  3. read write spinlock

    发一个自己基于 C++11 写的 read write spinlock,在 MinGW 4.8.2 (gcc 4.8 全面支持c++ 11,但由于gcc windows平台 libstdc++ 目前 ...

  4. poj 1458 Common Subsequence_最长公共子串

    题意:略 求最长公共子串 #include<iostream> #include<cstdio> #include<string> using namespace ...

  5. Centos下需安装Pytnon,Pytharm

    1.在www.python.org/PIPY/下载python3.4.2.tar.gz 2.在安装之前最好先安装相关的开发工具 # yum groupinstall develtools # yum ...

  6. Dollar Dayz(大数母函数,高低位存取)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5655   Accepted: 2125 Descr ...

  7. activity-alias的使用

    activity-alias是android里为了反复使用Activity而设计的. 当在Activity的onCreate()方法里,运行getIntent().getComponent().get ...

  8. WIN7 以下创建cocos2d-x3.0+lua项目

    用命令行生成和执行项目 无需打开VS 配置完环境 CMD执行 cocos new  helloWold   -p com.test -l lua -d E:\cocos2dx 来创建项目 cocos ...

  9. Java面试题之八

    四十一.面向对象的特征有哪些方面 四大特征大家都知道:抽象.继承.封装.多态.这是个理解性表述题,每个人的表述方式可能都不一样.下面仅选择一种作为参考: 1.抽象: 抽象——就是忽略一个主题中与当前目 ...

  10. Spark里面的任务调度:离SparkContext开始

    SparkContext这是发达国家Spark入学申请,它负责的相互作用和整个集群,它涉及到创建RDD.accumulators and broadcast variables.理解力Spark架构, ...