函数
为了减少重复的代码, 避免程序冗余, 增加代码的可读性
定义格式
返回值 函数名(参数列表){
函数体
}
- 返回值 : 有返回(返回对应的类型, 需要使用return )/无返回(void)
- 函数名: 起名要有语义, 最好使用小驼峰命名法(第一单词首字母小写, 其余首字母大写)
调用函数格式
函数名();
#include <iostream>
using namespace std;
// 1.定义函数: 函数定义之后需要调用才能有效果
void hello(){
cout << "hello c++"<< endl;
}
//
int main() {
// 调用函数
hello();
}
函数的形式
按照参数和返回值分类, 我们分为
- 无参数无返回
- 无参数有返回
- 有参数无返回
- 有参数有返回
#include <iostream>
using namespace std;
// 1.无参无返回
void hello() {
cout << "hello c++" << endl;
}
// 2.无参有返回
int showAge() {
return 100;
}
// 3.有参数无返回 int a, int b 是参数列表,
// 我们也称 a, b 为形参, 可以理解为声明站位
void add(int a, int b) {
cout << a << " + " << b << " = " << a + b << endl;
}
// 4.有参数有返回 1 ~ n 累加和
int calculateNum(int n){
int sum = 0;
for (int i = 1; i <= n ; ++i)
sum +=i;
return sum;
}
int main() {
hello();
int age = showAge();
cout << "age = " << age << endl;
add(10, 20); // 10, 20 这里叫实参
add(100, 200);
cout << "1 ~ 100 cal = " << calculateNum(100) << endl;
cout << "1 ~ 10 cal = " << calculateNum(10) << endl;
cout << "1 ~ 50 cal = " << calculateNum(50) << endl;
// 1~50累加和, 1~20 累加和, 将得到的累加和进行相乘
int num1 = calculateNum(50);
int num2 = calculateNum(20);
int res = num1 * num2;
cout << "res = " << res << endl;
}
函数参数顺序
参数传的顺序会对计算结果产生影响(甚至会产生错误), 为了避免歧义出现,参数传递一定要注意顺序
#include <iostream>
using namespace std;
void show_info(string name , int age, string addr, string gender){
cout << "name : "<< name << endl;
cout << "gender : "<< gender << endl;
cout << "age : "<< age << endl;
cout << "addr : "<< addr << endl;
}
void show_info2(string name , int age, string addr, string gender="male"){
cout << "name : "<< name << endl;
cout << "gender : "<< gender << endl;
cout << "age : "<< age << endl;
cout << "addr : "<< addr << endl;
}
int main() {
//必须参数: 函数参数传入的顺序要和声明时保持一致, 否则会产生一起
show_info("eric", 19, "shengyang", "feamle");
cout << "======================================================================="<< endl;
// show_info("eric", 19, "male", "beijing"); 传参错误
// 默认参数: 函数可以设置默认参数, 这样这个参数可以传(传入的值)也可以不传(默认的值)
show_info2("bob", 88, "newyork"); // 此时gender="male"
cout << "======================================================================="<< endl;
show_info2("bob", 88, "newyork", "male"); // 此时gender="male"
}
函数传参的两种形式
- 值传递
- 所谓值传递,就是函数调⽤时实参将数值传⼊给形参
- 值传递时,如果形参发⽣改变,并不会影响实参
#include <iostream>
using namespace std;
//(函数需要在调用前进行声明), 如果没有, 需要使用函数的原型声明
// 函数的原型声明, 先声明, 后实现
// 没有函数体.只有函数名字和参数
void swap(int, int);
int main() {
// 交换两个变量的值
int a = 10, b = 20;
cout << "a = "<< a << endl;
cout << "b = "<< b << endl;
swap(a, b);
cout << "a = "<< a << endl;
cout << "b = "<< b << endl;
}
// 在main函数后面声明
void swap(int x, int y){
int temp;
temp = x;
x = y;
y = temp;
cout << "============swap()============"<< endl;
cout << "x = "<< x << endl;
cout << "y = "<< y << endl;
cout << "++x = "<< ++x << endl;
cout << "============swap()============"<< endl;
}
-
引用传递
引用传递: 如果改变参数的值, 会对原来的值产生影响
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main() {
int a = 10, b = 20;
cout << "a = "<< a << endl;
cout << "b = "<< b << endl;
swap(a, b);
cout << "a = "<< a << endl;
cout << "b = "<< b << endl;
}
// 引用传递值, 如果改变参数的值, 会对原来的值产生影响
// &取地址符号
void swap(int &x, int &y){
int temp;
temp = x;
x = y;
y = temp;
cout << "============swap()============"<< endl;
cout << "x = "<< x << endl;
cout << "y = "<< y << endl;
cout << "++x = "<< ++x << endl;
cout << "============swap()============"<< endl;
}
网友评论