#include<iostream>voidsimon(int);intmain(){usingnamespacestd;simon(3);//第1次调用,参数是3
cout<<"pick an integer"<<endl;intcount;cin>>count;simon(count);//第2次调用,参数是count,也就是输入的数字
cout<<"done"<<endl;return0;}voidsimon(intn){usingnamespacestd;cout<<"simon says touch your toes "<<n<<" times"<<endl;//函数的作用:打印出:simon says touch your toes " n " times
}
运行结果:
1
2
3
4
5
simon says touch your toes 3 times
pick an integer
6
simon says touch your toes 6 times
done
函数格式
1
2
3
4
函数头()
{
函数体
}
上面的函数头:void simon(int n),void标示simon()函数没有返回值
在多函数中使用using编译命令
之前的函数中包含下面的编译命令:
1
usingnamespacestd;
这是函数中因为使用类cout,需要访问位于命名空间std中的cout定义
我们可以将编译命令放在函数的外部,且位于函数的前面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>voidsimon(int);//函数申明
usingnamespacestd;voidsimon(intn)//函数定义
{cout<<"Simon says touch your toes "<<n<<" times"<<endl;}intmain(){simon(3);cout<<"Pick an integer:"<<endl;intcount;cin>>count;simon(count);cout<<"done"<<endl;return0;}