一、函数的作用
函数由多条语句构成,实现某种功能。main 是一个特殊的函数,称为主函 数。程序从 main 函数开始运行。除了 main 函数,还可以自定义别的函数。
二、函数的定义 与变量定义类似,函数必须在使用前定义。函数定义的形式如下:
返回值类型 函数名(参数列表)
{
函数具体内容
}
1、返回值类型: 可以是 int、double、char 等类型,表示函数需要向使用者传 回什么类型的值。也可以是 void 类型,表示函数不传回任何值。
2、函数名: 函数名和变量名一样,随意起,合法的字符可以是大写字母、小写 字母、数字(不能以数字开头)、下划线_。
3、参数列表: 表示函数需要使用者传入什么类型的值。
4、函数具体内容: 由多条语句构成,用于实现函数想要实现功能,例如输出字 符、计算结果。 注意:不能在函数里定义一个(非匿名的)函数。
#include <iostream>
using namespace std;
void hello()
{
cout << "上午好";
}
int main()
{
hello();
return 0;
}
注意:
1、hello后面有(),小括号里没有任何东西,表示hello函数没有参数,因为 hello函数不需要从外界传入什么值。即使函数没有参数,定义和调用时的小 括号()也不能省略不写。
2、void表示hello函数没有返回值,因为这里hello函数只是用于输出一些字 符,不需要向外界传出什么值。
3、通常来说,如果一个函数的功能只是用于输出,那么返回值应当设置为 void。
4、main函数在调用无参数无返回值的hello函数时,只需要用hello(),注意 前面没有void。
例题二
#include <iostream>
using namespace std;
void print()
{
cout << "*****" << endl;
}
int main()
{
print();
print();
return 0;
}
以上两题如果不用函数怎么更简单地用以前的内容表达呢?
#include <iostream>
using namespace std;
int main()
{
cout << "下午好";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
cout << "*****" << endl;
cout << "*****" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "ABCDE" << endl;
cout << "ABCDE" << endl;
return 0;
}
#include <iostream>
using namespace std;
void print()
{
cout << "*****" << endl;
}
int main()
{
for (int i = 0; i < 10; i++)
{
print();
}
return 0;
}
#include <iostream>
using namespace std;
void print()
{
cout << "*****" << endl;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
print();
}
return 0;
}
#include <iostream>
using namespace std;
void print1()
{
cout << "*****" << endl;
}
void print2()
{
cout << "#####" << endl;
}
int main()
{
print1();
print2();
return 0;
}
四、形参和实参
1、函数定义时声明的参数列表里的参数称为形参(形式参数)。形参分为非引 用(copy)形参和引用形参两种,默认是非引用形参。
2、函数调用时使用的值称为实参(实际参数)。非引用形参对应的实参必须是 右值,引用形参对应的实参必须是左值。
3、函数调用时,实参和形参建立起了联系。非引用形参拷贝了实参的值,引用 形参则是绑定了实参的地址(视为与实参是同一个左值)。 本节课只涉及非引用形参,引用形参将在下一节课中阐述。
4、在函数中,不能使用实参(因为看不见),可以使用形参。相反的,在主函 数里,可以访问到实参,但不可以访问函数里形参的值。
5、不可以在函数中再次定义与形参同名的变量,一般也不会输入值到形参中 (因为可经由实参传值)。通常情况下,由主函数负责输入值,函数调用时实 参负责把值传给形参。
6、在函数里,非引用形参可以当左值和右值。引用形参可以当左值,是否能当 右值取决于实参是否可以当右值,当然,在函数里如果对引用形参赋值后,也 可以当右值使用。
#include<iostream>
using namespace std;
void f(int n) //这里是函数的定义
{
cout << n + 2 << endl;
}
int main()
{
int a;
cin >> a;
f(a); //这里是函数的调用,a的值传给了函数定义部分的参数n
return 0;
}
说明:
1、代码中void f(int n)处的n是形参。形参列表中,每个形参都以一 个变量的形式标注,并且每个形参前面都要有类型声明。形参起什么名字都可以,也可以和实参同名,例如可以把函数f定义中的n都改成a。
2、代码中主函数f(a)处的a是实参。由于对应的是非引用形参,故实参必须是右值,f(a); f(10); f(a+1); 都是可以的。
#include<iostream>
using namespace std;
void f(int a, int b)
{
cout << a + b << endl;
}
int main()
{
int n;
cin >> n;
while (n--)//多组案例
{
int a, b;
cin >> a >> b;
f(a, b);
}
return 0;
}
说明: void f(int a, int b) 不可以写成 void f(int a, b)
#include<iostream>
using namespace std;
void f(int n)
{
for (int i = 1; i <= n; i++)
{
cout << i;
if (i == n)
{
cout << endl;
}
else
{
cout << ‘ ';
}
}
}
int main()
{
int m;
cin >> m;
f(m);
return 0;
}
说明: 一般情况下,不会在自定义函数里做输入(cin)。由主函数负责输入,然后通过传参数值的方式传给函数运行。
#include<iostream>
using namespace std;
void f(int n)
{
for (int i = 0; i < n; i++)
{
cout << '*’;
}
cout << endl;
}
int main()
{
int m;
cin >> m;
f(m);
return 0;
}
#include<iostream>
using namespace std;
void f(int a, int b)
{
int c = a * a + 1;
while (c % b != 0)
{
c++;
}
cout << c << endl;
}
int main()
{
int x, y;
cin >> x >> y;
f(x, y);
return 0;
}
#include<iostream>
using namespace std;
void f(char n)
{
if (n >= 'A' && n <= 'Z’)
{
cout << (char)(n + 32) << endl;
}
else if (n >= 'a' && n <= 'z’)
{
cout << (char)(n - 32) << endl;
}
else
{
cout << n << endl;
}
}
int main()
{
char a;
cin >> a;
f(a);
return 0;
}
说明:
通常情况下,当函数的功能是计算出某个结果时(例 7、例 8、例 11、例 12),也比较少在函数里做输出(cout),更合理的做法是用有返回值的函数来实现。