函数是一个可以实现特定功能不断重复使用的一系列语句。前面已经说过每个程序都必须有一个主函数:main()。然而多数程序都用到了很多个函数。
通常来说,你的程序有时候在运行的时候可能需要中断一下去做其他事情。你在生活中也常常遇到这些事情,举个例子。当你读书时,你接到了一个电话。这个时候你就需要对书做一个标记,然后去接电话,接完电话后再去接着读书。
C++程序也是如此,程序都是按顺序执行着,知道遇到一个函数调用。函数调用是一个标记,道速CPU暂时中断执行现有程序,切换到另一个函数继续执行。CPU会标记一下现在执行的地点,然后去调用相应的函数。当调用的函数执行完成以后,程序再从标记的地方继续执行。
启动函数调用的函数称作调用方,被调用的函数叫做被调用方。下面是一个例子:
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream> // for std::cout and std::endl
// Definition of function doPrint()
void doPrint() // doPrint() is the called function in this example
{
std::cout << "In doPrint()" << std::endl;
}
// Definition of function main()
int main()
{
std::cout << "Starting main()" << std::endl;
doPrint(); // Interrupt main() by making a function call to doPrint(). main() is the caller.
std::cout << "Ending main()" << std::endl;
return 0;
}
上面的函数输出是:
Starting main()
In doPrint()
Ending main()
该函数的执行过程如下:首先执行main()函数,直到doPrint,此时CPU会暂停main函数的执行,并做标记,去执行doPrint()函数。该函数执行完成之后,再次从中断前标记的地方执行main()函数。
你可以看到函数的调用就是调用函数的名称,例子中的函数并没有参数,带有参数的函数后面会讲到。这里一定要记住,不要忘了括号。
返回值:
如上面的例子当主函数停止执行的时候,会返回一个0给系统,这个0就是返回值。
当你写自己的程序的时候,返回值可以有,也可以没有,根据函数的功能所定。这个可以通过在函数名称前增加一个数据类型来做,如果没有就用void,有就可用int,char等等。这个类型并不代表一定返回某个特定的值,而是这种类型的任意值都可以。
然后我们在函数里面使用return语句来返回相应的数据类型给调用方,如下面的例子:
// int means the function returns an integer value to the caller
int return5()
{
// this function returns an integer, so a return statement is needed
return 5; // we're going to return integer value 5 back to the caller of this function
}
int main()
{
std::cout << return5() << std::endl; // prints 5
std::cout << return5() + 2 << std::endl; // prints 7
return5(); // okay: the value 5 is returned, but is ignored since main() doesn't do anything with it
return 0;
}
函数int return5()的返回值类型是int类型,通过return语句也返回来int类型数据:5.
返回值为void类型
有时候函数不需要返回具体的值,这个时候就需要通过void类型来告诉调用者,该函数不反悔任何具体的值。如:
void doPrint() // void is the return type
{
std::cout << "In doPrint()" << std::endl;
// This function does not return a value so no return statement is needed
}
因为没有具体的返回值,也就不需要return语句了。
参考下面的例子:
// void means the function does not return a value to the caller
void returnNothing()
{
std::cout << "Hi" << std::endl;
// This function does not return a value so no return statement is needed
}
int main()
{
returnNothing(); // okay: function returnNothing() is called, no value is returned
std::cout << returnNothing(); // error: this line will not compile. You'll need to comment it out to continue.
return 0;
}
在第一次调用的时候,函数将会正常调用returnNothing,打印Hi。但是第二次调用的时候编译器会报错。因为我们的returnNothing函数不会返回任何值。但是std::cout需要输出值到屏幕上,这就编译报错了。
主函数的返回:
你现在应该有一个简单的概念main()函数是如何执行的。当程序执行的时候,首先跑到main函数中执行。main函数执行完成之后,都会返回一个0给系统,所以我们命名是:int main()。
为什么main()函数要返回0呢,这个0称作状态码。告诉系统程序的运行状态,如果是0表示程序运行成功。如果是其他值,标识程序出问题了。
C++中定义了main必须有一个int类型的返回值。所以建议你在写main()函数的时候,需要默认写为int main()。
同时建议将main()函数写在程序的最下面,这个我们会在后面将为什么。
关于返回值一些其他的注意事项:
首先如果你定义一个函数是有返回值的,那么必须使用return语句返回相应类型的值。main函数除外,因为可能main函数你不返回的话,系统会默认给你返回0.
其次,你需要知道一旦调用return语句,那么该函数就结束了,任何之后的多余代码都不会执行。
再次,一个函数只能返回一个值。函数可能会使用自己的逻辑控制决定返回哪一个值。函数可以返回一个值,一个变量或者一个声明,或者 会从一堆可能的值中选择其中的一个返回。
当然想要从函数中获取多个值的方案也是可行的,这个我们会在后面进行深入的探讨。
最后,返回值可以任意地定义成你需要的样子。一些函数用返回值来返回程序的状态码,一些用来返回计算的结果,一些不用返回值。因为返回值多种多样,所以建议写程序的时候最好备注一下返回值的意思。
复用的函数:
一些函数在程序里会重复使用多次,当然这也是为什么把这部分抽象成函数的原因。
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
// getValueFromUser will read a value in from the user, and return it to the caller
int getValueFromUser()
{
std::cout << "Enter an integer: "; // ask user for an integer
int a; // allocate a variable to hold the user input
std::cin >> a; // get user input from console and store in variable a
return a; // return this value to the function's caller (main)
}
int main()
{
int x = getValueFromUser(); // first call to getValueFromUser
int y = getValueFromUser(); // second call to getValueFromUser
std::cout << x << " + " << y << " = " << x + y << std::endl;
return 0;
}
这个函数就使用了getValueFromUser两次,目的是输入值给变量。需要注意的是不只main函数可以调用其他函数,任意的函数都可以调用其他函数,如下面的例子:
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
void printA()
{
std::cout << "A" << std::endl;
}
void printB()
{
std::cout << "B" << std::endl;
}
// function printAB() calls both printA() and printB()
void printAB()
{
printA();
printB();
}
// Definition of main()
int main()
{
std::cout << "Starting main()" << std::endl;
printAB();
std::cout << "Ending main()" << std::endl;
return 0;
}
嵌套函数:在C++里面,函数不能在另外一个函数里面定义,也就是嵌套定义。下面的例子是非法的:
#include <iostream>
int main()
{
int foo() // this function is nested inside main(), which is illegal.
{
std::cout << "foo!" << std::endl;
return 0;
}
foo();
return 0;
}
正确地写法如下:
#include <iostream>
int foo() // no longer inside of main()
{
std::cout << "foo!" << std::endl;
return 0;
}
int main()
{
foo();
return 0;
}