在本教程中,我们将借助示例学习 C++ 函数和函数表达式。
函数是执行特定任务的代码块。
假设我们需要创建一个程序来创建一个圆并为其着色。 我们可以创建两个函数来解决此问题:
- 画圆的函数
- 给圆上色的函数
将复杂的问题分成较小的块可以使我们的程序易于理解和重用。
有两种函数:
- 标准库函数:在 C++ 中预定义
- 用户定义的函数:由用户创建
在本教程中,我们将主要关注用户定义的函数。
C++ 用户定义函数
C++ 允许程序员定义自己的函数。
用户定义的函数将执行特定任务的代码分组,并为该组代码指定名称(标识符)。
从程序的任何部分调用该函数时,它们都将执行该函数主体中定义的代码。
C++ 函数声明
声明函数的语法为:
returnType functionName (parameter1, parameter2,...) {// function body}
这是一个函数声明的例子。
// function declarationvoid greet() {cout << "Hello World";}
这里,
- 该函数的名称为
greet() - 该函数的返回类型为
void - 空括号表示没有任何参数
- 函数体写在
{}内部
注意:我们将在本教程的后面部分学习returnType和parameters。
调用函数
在上面的程序中,我们声明了一个名为greet()的函数。 要使用greet()函数,我们需要对其进行调用。
这就是我们可以调用上面的greet()函数的方法。
int main() {// calling a functiongreet();}

函数如何在 C++ 中工作
示例 1:显示文本
#include <iostream>using namespace std;// declaring a functionvoid greet() {cout << "Hello there!";}int main() {// calling the functiongreet();return 0;}
输出
Hello there!
函数参数
如上所述,可以使用参数(参数)声明函数。 参数是在声明函数时传递的值。
例如,让我们考虑以下函数:
void printNum(int num) {cout << num;}
在此,int变量num是函数参数。
我们在调用函数时将值传递给函数参数。
int main() {int n = 7;// calling the function// n is passed to the function as argumentprintNum(n);return 0;}
示例 2:带参数的函数
// program to print a text#include <iostream>using namespace std;// display a numbervoid displayNum(int n1, float n2) {cout << "The int number is " << n1;cout << "The double number is " << n2;}int main() {int num1 = 5;double num2 = 5.5;// calling the functiondisplayNum(num1, num2);return 0;}
输出
The int number is 5The double number is 5.5
在上面的程序中,我们使用了一个函数,该函数具有一个int参数和一个double参数。
然后,我们将num1和num2作为参数传递。 这些值分别通过函数参数n1和n2存储。

带参数的 C++ 函数
注意:调用函数时传递的参数类型必须与函数声明中定义的相应参数匹配。
return语句
在以上程序中,我们在函数声明中使用了void。 例如,
void displayNumber() {// code}
这意味着该函数不返回任何值。
也可以从函数返回值。 为此,我们需要在函数声明期间指定函数的returnType。
然后,return语句可用于从函数返回值。
例如,
int add (int a, int b) {return (a + b);}
在这里,我们具有数据类型int而不是void。 这意味着该函数返回int值。
代码return (a + b);返回两个参数的总和作为函数值。
return语句表示函数已结束。 函数内return之后的任何代码均不执行。
示例 3:加两个数字
// program to add two numbers using a function#include <iostream>using namespace std;// declaring a functionint add(int a, int b) {return (a + b);}int main() {int sum;// calling the function and storing// the returned value in sumsum = add(100, 78);cout << "100 + 78 = " << sum << endl;return 0;}
输出
100 + 78 = 178
在上述程序中,add()函数用于查找两个数字的和。
在调用函数时,我们传递了两个int字面值100和78。
我们将函数的返回值存储在变量sum中,然后进行打印。

C++ 函数与return语句的配合使用
注意,sum是int类型的变量。 这是因为add()的返回值是int类型的。
函数原型
在 C++ 中,函数声明的代码应在函数调用之前。 但是,如果要在函数调用之后定义函数,则需要使用函数原型。 例如,
// function prototypevoid add(int, int);int main() {// calling the function before declaration.add(5, 3);return 0;}// function definitionadd(int a, int b) {cout < (a + n);}
在上面的代码中,函数原型为:
void add(int, int);
这为编译器提供了有关函数名称及其参数的信息。 这就是为什么我们可以在定义函数之前使用代码来调用函数。
函数原型的语法为:
returnType functionName(dataType1, dataType2, ...);
示例 4:C++ 函数原型
// using function definition after main() function// function prototype is declared before main()#include <iostream>using namespace std;// function prototypeint add(int, int);int main() {int sum;// calling the function and storing// the returned value in sumsum = add(100, 78);cout << "100 + 78 = " << sum << endl;return 0;}// function definitionint add(int a, int b) {return (a + b);}
输出
100 + 78 = 178
上面的程序与示例 3 几乎相同。 唯一的区别是,此处在函数调用之后定义了函数。
这就是我们在此示例中使用函数原型的原因。
使用用户定义函数的好处
- 函数使代码可重用。 我们可以声明一次并多次使用它们。
- 通过将每个小任务划分为一个函数,函数使程序更容易。
- 函数增加了可读性。
C++ 库函数
库函数是 C++ 编程中的内置函数。
程序员可以通过直接调用库函数来使用它们; 他们不需要自己编写函数。
C++ 中一些常见的库函数是sqrt(),abs(),isdigit()等。
为了使用库函数,我们通常需要包括在其中定义这些库函数的头文件。
例如,为了使用诸如sqrt()和abs()之类的数学函数,我们需要包括头文件cmath。
示例 5:查找数字的平方根的 C++ 程序
#include <iostream>#include <cmath>using namespace std;int main() {double number, squareRoot;number = 25.0;// sqrt() is a library function to calculate the square rootsquareRoot = sqrt(number);cout << "Square root of " << number << " = " << squareRoot;return 0;}
输出
Square root of 25 = 5
在此程序中,sqrt()库函数用于计算数字的平方根。
sqrt()的函数声明在cmath头文件中定义。 这就是为什么我们需要使用代码#include <cmath>来使用sqrt()函数的原因。
要了解更多信息,请访问 C++ 标准库函数。
