调用前先声明函数:
>>若函数定义在调用点之前,则无需另外声明
>>若函数定义在调用点之后,则需要在调用函数前按如下形式声明函数原型(原型声明)
类型标识符 被调用函数名(含类型说明的形参表)
调用形式
函数名(实参列表)
嵌套调用
在一个函数的函数体中。可以调用另一函数,称为嵌套调用
递归调用:
函数直接或间接调用自身
例一:编写一个求x的n次方的函数
#include<iostream>
using namespace std;
double power(double x, int n)
{
double val = 1.0;
while (n--)
val *= x;
return val;
}
int main()
{
cout << "5 to the power 2 is "<<power(5,2) << endl;
system("pause");
return 0;
}
输出结果为25
例二:数制转换:
输入一个8位二进制数,将其转化为十进制数输出。
#include<iostream>
using namespace std;
double power(double x, int n);//计算x的n次方
int main()
{
int value = 0;
cout << "Enter an 8 bit binary number ";
for (int i = 7; i >= 0; i--)
{
char ch;
cin >> ch;
if (ch == '1')
value += static_cast<int>(power(2, i));
}
cout << "Decimal value is " << value << endl;
system("pause");
return 0;
}
double power(double x, int n)
{
double val = 1.0;
while (n--)
val *= x;
return val;
}
输出结果:
例三:编写程序求π的值
π = 16arctan(1/5)-4arctan(1/239)
arctanx = x - x3/3 + x5/5 - x7/7直到级数某项绝对值不大于10-15为止,π和x均为double型
#include<iostream>
using namespace std;
double arctan(double x)
{
double sqr = x * x;
double e = x;
double r = 0;
int i = 1;
while (e / i > 1e-15)
{
double f = e / i;
r = (i % 4 == 1) ? r + f : r - f;
e = e * sqr;
i += 2;
}
return r;
}
int main()
{
double a = 16.0 * arctan(1 / 5.0);
double b = 4.0 * arctan(1 / 239.0);
cout << "PI = " << a - b << endl;
return 0;
}#include<iostream>
using namespace std;
double arctan(double x)
{
double sqr = x * x;
double e = x;
double r = 0;
int i = 1;
while (e / i > 1e-15)
{
double f = e / i;
r = (i % 4 == 1) ? r + f : r - f;
e = e * sqr;
i += 2;
}
return r;
}
int main()
{
double a = 16.0 * arctan(1 / 5.0);
double b = 4.0 * arctan(1 / 239.0);
cout << "PI = " << a - b << endl;
return 0;
}
输出结果为3.14159
例四:
寻找并输出11-999之间的数m,他满足m,m2,m3都是回文数
#include<iostream>
using namespace std;
bool symm(unsigned n)
{
unsigned i = n;
unsigned m = 0;
while (i > 0)
{
m = m * 10 + i % 10;
i /= 10;
}
return m == n;
}
int main()
{
for(unsigned m = 11;m<1000;m++)
if (symm(m) && symm(m*m) && symm(m*m*m))
{
cout << "m =" << m;
cout << "m * m =" << m * m;
cout << "m * m * m =" << m * m * m << endl;
}
system("pause");
return 0;
}
输出结果:
例五:
#include<iostream>
#include<cmath>
using namespace std;
const double TINY_VALUE = 1e-10;
double tsin(double x)
{
double g = 0;
double t = x;
int n = 1;
do
{
g += t;
n++;
t = -t * x * x / (2 * n - 1) / (2 * n - 2);
} while (fabs(t) >= TINY_VALUE);
return g;
}
int main()
{
double k, r, s;
cout << "r =";
cin >> r;
cout << "s =";
cin >> s;
if (r * r <= s * s)
k = sqrt(tsin(r)*tsin(r) + tsin(s)*tsin(s));
else
k = tsin(r * s) / 2;
cout << k << endl;
system("pause");
return 0;
}
输出结果:
嵌套调用
嵌套调用举例:
输入两个整数,求平方和:
#include<iostream>
using namespace std;
int fun2(int m)
{
return m * m;
}
int fun1(int x, int y)
{
return fun2(x) + fun2(y);
}
int main()
{
int a, b;
cout << "Please enter two integers(a and b): ";
cin >> a >> b;
cout << "The sum of square of a nad b: " << fun1(a, b) << endl;
system("pause");
return 0;
}
递归调用:
函数直接或间接地调用自身,称为递归调用、
递归过程的两个阶段:
递归调用举例:
求n!
#include<iostream>
using namespace std;
unsigned fac(int n)
{
unsigned f;
if (n == 0)
f = 1;
else
f = fac(n - 1) * n;
return f;
}
int main()
{
unsigned n;
cout << "Enter a positive integer: ";
cin >> n;
unsigned y = fac(n);
cout << n << "!=" << y << endl;
system("pause");
return 0;
}
阶乘问题与或图
举例如下:
用递归发计算从n个人中选择k个人组成一个委员会的不同 组合数
分析:
>>由n个人里选k个人的组合数
= 由n-1个人里选k个人的组合数 + 由m-1个人里选k-1个人的组合数
当n = k 或k = 0时,组合数为1
#include<iostream>
using namespace std;
int comm(int n, int k)
{
if (k > n)
return 0;
else if (n == k || k == 0)
return 1;
else
return comm(n - 1, k) + comm(n - 1, k - 1);
}
int main()
{
int n, k;
cout << "Please enter two integers n and k: ";
cin >> n >> k;
cout << "C(n,k) = " << comm(n,k) << endl;
system("pause");
return 0;
}
输出结果: