1.声明、定义和使用函数
#include <iostream>
using namespace std;
void show(); //函数声明语句
void main()
{
show(); //函数调用
system("pause");
}
void show() //函数定义
{
cout << "爱你,胖胖" << endl;
}
结果:
2.调用默认参数的函数,
实例输出两行字符串,一行是函数默认参数,一行是通过传字符串实参。
#include <iostream>
using namespace std;
void show(const char* Data = "Pangpang,I love you")
{
cout << Data << endl;
}
void main()
{
show(); //默认
show("Pangpang,I miss you"); //直接
system("pause");
}
结果:
3.定义省略号形式的函数参数
#include <iostream>
#include <STDARG.H>
using namespace std;
void Outputinfo(int num,...)
{
va_list argument;
va_start(argument, num);
while (num--)
{
char* pchData = va_arg(argument, char*);
int iData = va_arg(argument, int);
cout << pchData << endl;
cout << iData << endl;
}
va_end(argument);
}
void main()
{
Outputinfo(2, "B", 2008, "O", 2008);
system("pause");
}
结果:
4.函数调用
传值调用、嵌套调用和递归调用
5.使用传值调用
#include <iostream>
using namespace std;
void swap(int a,int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
void main()
{
int x, y;
cout << "输入两个数:" << endl;
cin >> x;
cin >> y;
if (x < y)
{
swap(x, y);
}
cout << "x="<<x<< endl;
cout << "y="<<y<< endl;
system("pause");
}
结果:
并不能实现
6.嵌套调用
#include <iostream>
using namespace std;
void Showlove()
{
cout << "I love you,Pangpang" << endl;
}
void Zong()
{
Showlove();
}
void main()
{
Zong();
system("pause");
}
结果:
7.递归调用
求n的阶乘
#include <iostream>
using namespace std;
int show(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n*show(n - 1);
}
}
void main()
{
int n;
cout << "Please input a number:" << endl;
cin >> n;
cout << show(n);
system("pause");
}
结果:
8利用循环求你n的阶乘。
#include <iostream>
using namespace std;
int show(int n)
{
int i,j=1;
for ( i = 1; i <= n;i++)
{
j*=i;
}
return j;
}
void main()
{
int n;
int f;
cout << "Please input a number:" << endl;
cin >> n;
f=show(n);
cout << f;
system("pause");
}
结果:
10.重载函数
#include <iostream>
using namespace std;
//int Add();
//double Add(); //重载时无法声明(不知道为什么)
int Add(int a, int b)
{
cout << "int add" << endl;
return a + b;
}
double Add(double a, double b)
{
cout << "double add" << endl;
return a + b;
}
void main()
{
int x = Add(5, 2);
float y = Add(10.5, 11.4);
system("pause");
}
结果:
11.内联函数
只起优化作用
#include <iostream>
using namespace std;
inline int IntegerAdd(int x, int y);
void main()
{
int a=1;
int b=1;
int result= IntegerAdd(a,b);
system("pause");
}
int IntegerAdd(int x, int y)
{
return x + y;
}
12.变量的存储类别
auto变量、static变量、register变量、extern变量
使用static变量实现累加
#include <iostream>
using namespace std;
int add(int x)
{
static int n = 0;
n = n + x;
return n;
}
void main()
{
int i, j, sum;
cout << "input the number:" << endl;
cin >> i;
cout << "the result is:" << endl;
for (j = 1; j <= i; j++)
{
sum = add(j);
cout << j << ":" << sum << endl;
}
system("pause");
}
结果:
#include <iostream>
using namespace std;
int add(int x)
{
int n = 0;
n = n + x;
return n;
}
void main()
{
int i, j, sum;
cout << "input the number:" << endl;
cin >> i;
cout << "the result is:" << endl;
for (j = 1; j <= i; j++)
{
sum = add(j);
cout << j << ":" << sum << endl;
}
system("pause");
}
结果: