函数是一个做某项工作的报表设计序列。 你已经知道,每一个程序必须有一个名为main()的。 然而,大多数程序有很多的功能,他们都类似于主要工作。
通常情况下,你的程序需要,中断暂时做别的事情,它是做什么的。 您在现实生活中做到这一点,所有的时间。 例如,你可能会读一本书时,你还记得你需要拨打一个电话。 你把你的书的书签,使手机通话,当你正在做的电话,你回到你的书,你离开的地方。
C + +程序的工作方式相同。 程序将执行语句顺序一个函数里面,当它遇到一个函数调用。 函数调用是一个表达式,告诉CPU中断当前的功能和执行其他功能。 把CPU的“书签”在当前执行点,然后调用 (执行)在函数调用的函数名为。 当被调用函数结束时,CPU可以追溯到它书签点,并恢复执行。
这里是一个示例程序,说明如何声明,并呼吁新功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
// we need this in each function that uses cout and endl
cout <<
"In DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
// we need this in each function that uses cout and endl
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output:
This program begins execution at the top of main(), and the first line to be executed prints Starting main()
. The second line in main is a function call to DoPrint. At this point, execution of statements in main() is suspended, and the CPU jumps to DoPrint(). The first (and only) line in DoPrint prints In DoPrint()
. When DoPrint() terminates, the caller (main()) resumes execution where it left off. Consequently, the next statment executed in main prints Ending main()
.
Functions can be called multiple times:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
cout <<
"In DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
DoPrint();
// This is a function call to DoPrint()
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output:
In this case, main() is interrupted 3 times, once for each call to DoPrint().
Main isn’t the only function that can call other functions. In the following example, DoPrint() calls a second function, DoPrint2().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
void
DoPrint2()
{
using
namespace
std;
cout <<
"In DoPrint2()"
<< endl;
}
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
cout <<
"Starting DoPrint()"
<< endl;
DoPrint2();
// This is a function call to DoPrint2()
DoPrint2();
// This is a function call to DoPrint2()
cout <<
"Ending DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output:
函数是一个做某项工作的报表设计序列。 你已经知道,每一个程序必须有一个名为main()的。 然而,大多数程序有很多的功能,他们都类似于主要工作。
通常情况下,你的程序需要,中断暂时做别的事情,它是做什么的。 您在现实生活中做到这一点,所有的时间。 例如,你可能会读一本书时,你还记得你需要拨打一个电话。 你把你的书的书签,使手机通话,当你正在做的电话,你回到你的书,你离开的地方。
C + +程序的工作方式相同。 程序将执行语句顺序一个函数里面,当它遇到一个函数调用。 函数调用是一个表达式,告诉CPU中断当前的功能和执行其他功能。 把CPU的“书签”在当前执行点,然后调用 (执行)在函数调用的函数名为。 当被调用函数结束时,CPU可以追溯到它书签点,并恢复执行。
这里是一个示例程序,说明如何声明,并呼吁新功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
// we need this in each function that uses cout and endl
cout <<
"In DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
// we need this in each function that uses cout and endl
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output:
This program begins execution at the top of main(), and the first line to be executed prints Starting main()
. The second line in main is a function call to DoPrint. At this point, execution of statements in main() is suspended, and the CPU jumps to DoPrint(). The first (and only) line in DoPrint prints In DoPrint()
. When DoPrint() terminates, the caller (main()) resumes execution where it left off. Consequently, the next statment executed in main prints Ending main()
.
Functions can be called multiple times:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
cout <<
"In DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
DoPrint();
// This is a function call to DoPrint()
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output:
In this case, main() is interrupted 3 times, once for each call to DoPrint().
Main isn’t the only function that can call other functions. In the following example, DoPrint() calls a second function, DoPrint2().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
void
DoPrint2()
{
using
namespace
std;
cout <<
"In DoPrint2()"
<< endl;
}
// Declaration of function DoPrint()
void
DoPrint()
{
using
namespace
std;
cout <<
"Starting DoPrint()"
<< endl;
DoPrint2();
// This is a function call to DoPrint2()
DoPrint2();
// This is a function call to DoPrint2()
cout <<
"Ending DoPrint()"
<< endl;
}
// Declaration of main()
int
main()
{
using
namespace
std;
cout <<
"Starting main()"
<< endl;
DoPrint();
// This is a function call to DoPrint()
cout <<
"Ending main()"
<< endl;
return
0;
}
|
This program produces the following output: