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

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

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

//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. 为TL-WR720N编译带mentohust和njit-client的openwrt固件

    openwrt的trunk版已经支持720N了.简单好多. 首先下载openwrt源码,我下的是trunk版 svn co svn://svn.openwrt.org/openwrt/trunk/ 然 ...

  2. 【思考题】CSDN第四届在线编程大赛2014初赛:带通配符的数

    题目要求: 输入参数:参数A,含有任意个数的?的数值字符串,如:12?4,?代表一位任意数                     参数B,不含?的数值字符串,长度与参数A一致 输出结果:参数A比参数 ...

  3. unity3d导出pdf

    unity生成pdf格式,首先需要导入iTextSharp.dll ,下面是我写的一些方法,可以直接用.直接贴代码, using UnityEngine; using System.Collectio ...

  4. 写一个将当前页面 URL 中的 get 参数解析成一个对象的方法。

    function getQuery () { var args = {}; var query = window.location.search.substring(1); var pairs = q ...

  5. magento产品导入时需要注意的事项

    (1) 必须保证csv文件是utf-8编码的.非utf-8的编码会导致产品导入失败 (2)产品图片 (a) 产品图片必须包含image,image_label,_media_image,_media_ ...

  6. BZOJ 3181([Coci2012]BROJ-最小质因子为p的第k小素数)

    3181: [Coci2012]BROJ Time Limit: 10 Sec   Memory Limit: 64 MB Submit: 26   Solved: 7 [ Submit][ Stat ...

  7. 浅析C# 中object sender与EventArgs e (转)

    一.了解C#中的预定义事件处理机制    在写代码前我们先来熟悉.net框架中和事件有关的类和委托,了解C#中预定义事件的处理. EventArgs是包含事件数据的类的基类,用于传递事件的细节. Ev ...

  8. .NET系统架构改造的经验和教训

    转自: http://robbinfan.com/blog/43/rid-off-dotnet-experience 在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方 ...

  9. 动态代理双剑客--JDK Proxy与CGLIB

    背景: 研究过设计模式的同胞们都知道代理模式可以有两种实现方案: 1.接口实现(或继承抽象类) 核心代码片段 ProxySubject-->>doOperation() //dosomet ...

  10. J2EE学习的一部分--JDBC详细说明

    今天是关于我们JDBC相关知识,左右JDBC我想大家都很熟悉的,我记得在很早以前就开始使用它,我记得那是一个大二的学生做课程设计.但随后以完成任务,所以遇到的问题google,当时没有时间组织,下关于 ...