加载中...

3.6 定义与使用


将前面章节所有的代码片段集中到一起后,整个程序如下:

  1. #include <iostream.h>
  2. void newLine ()
  3. {
  4.   cout << endl;
  5. }
  6. void threeLine ()
  7. {
  8.   newLine (); newLine (); newLine ();
  9. }
  10. void main ()
  11. {
  12. cout << "First Line." << endl;
  13. threeLine ();
  14. cout << "Second Line." << endl;
  15. }

这段程序包含3个函数定义:newLine、threeLine和main。

main函数内有一条语句使用(调用)了threeLine。同样的,threeLine调用了三次newLine。请注意,每个函数定义都出现在调用之前。

在C++中这是必需的:函数的定义必须出现在第一次使用之前(之上)。你可以把函数顺序调换一下,然后尝试编译程序,看得到什么错误信息。


还没有评论.