一个函数是一个序列的设计做一个特定工作表。你已经知道,每个程序都必须有一个功能叫做main()。然而,大多数程序有许多功能,和他们所有的工作类似于主。
Often, your program needs to interrupt what it is doing to temporarily do something else. You do this in real life all the time. For example, you might be reading a book when you remember you need to make a phone call. You put a bookmark in your book, make the phone call, and when you are done with the phone call, you return to your book where you left off.
通常,你的程序需要打断它是什么做的临时做其他的事情。你在现实生活中所有的时间。例如,你可能读一本书时,你记得你需要打一个电话。你把一个书签在你的书中,打电话,当你完成的电话,你回到你的书你离开的地方了。
C++ programs work the same way. A program will be executing statements sequentially inside one function when it encounters a function call. A function call is an expression that tells the CPU to interrupt the current function and execute another function. The CPU “puts a bookmark” at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.
C++程序的工作方式相同。一个程序将执行语句顺序在一个函数在遇到函数调用时。一个函数调用是一个表达式,告诉CPU中断当前的功能和执行另一个函数。该CPU”把书签”在当前执行点,然后调用(执行)在函数调用中指定的功能。当调用函数终止,该CPU回到点的书签,并继续执行。
Here is a sample program that shows how new functions are declared and called
下面是一个示例程序,展示了如何新函数的声明和调用
//#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;
}