函數(shù)
為了減少重復(fù)的代碼, 避免程序冗余, 增加代碼的可讀性
定義格式
返回值 函數(shù)名(參數(shù)列表){
函數(shù)體
}
- 返回值 : 有返回(返回對(duì)應(yīng)的類型, 需要使用return )/無(wú)返回(void)
- 函數(shù)名: 起名要有語(yǔ)義, 最好使用小駝峰命名法(第一單詞首字母小寫, 其余首字母大寫)
調(diào)用函數(shù)格式
函數(shù)名();
#include <iostream>
using namespace std;
// 1.定義函數(shù): 函數(shù)定義之后需要調(diào)用才能有效果
void hello(){
cout << "hello c++"<< endl;
}
//
int main() {
// 調(diào)用函數(shù)
hello();
}
函數(shù)的形式
按照參數(shù)和返回值分類, 我們分為
- 無(wú)參數(shù)無(wú)返回
- 無(wú)參數(shù)有返回
- 有參數(shù)無(wú)返回
- 有參數(shù)有返回
#include <iostream>
using namespace std;
// 1.無(wú)參無(wú)返回
void hello() {
cout << "hello c++" << endl;
}
// 2.無(wú)參有返回
int showAge() {
return 100;
}
// 3.有參數(shù)無(wú)返回 int a, int b 是參數(shù)列表,
// 我們也稱 a, b 為形參, 可以理解為聲明站位
void add(int a, int b) {
cout << a << " + " << b << " = " << a + b << endl;
}
// 4.有參數(shù)有返回 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 這里叫實(shí)參
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 累加和, 將得到的累加和進(jìn)行相乘
int num1 = calculateNum(50);
int num2 = calculateNum(20);
int res = num1 * num2;
cout << "res = " << res << endl;
}
函數(shù)參數(shù)順序
參數(shù)傳的順序會(huì)對(duì)計(jì)算結(jié)果產(chǎn)生影響(甚至?xí)a(chǎn)生錯(cuò)誤), 為了避免歧義出現(xiàn),參數(shù)傳遞一定要注意順序
#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() {
//必須參數(shù): 函數(shù)參數(shù)傳入的順序要和聲明時(shí)保持一致, 否則會(huì)產(chǎn)生一起
show_info("eric", 19, "shengyang", "feamle");
cout << "======================================================================="<< endl;
// show_info("eric", 19, "male", "beijing"); 傳參錯(cuò)誤
// 默認(rèn)參數(shù): 函數(shù)可以設(shè)置默認(rèn)參數(shù), 這樣這個(gè)參數(shù)可以傳(傳入的值)也可以不傳(默認(rèn)的值)
show_info2("bob", 88, "newyork"); // 此時(shí)gender="male"
cout << "======================================================================="<< endl;
show_info2("bob", 88, "newyork", "male"); // 此時(shí)gender="male"
}
函數(shù)傳參的兩種形式
- 值傳遞
- 所謂值傳遞,就是函數(shù)調(diào)?時(shí)實(shí)參將數(shù)值傳?給形參
- 值傳遞時(shí),如果形參發(fā)?改變棘捣,并不會(huì)影響實(shí)參
#include <iostream>
using namespace std;
//(函數(shù)需要在調(diào)用前進(jìn)行聲明), 如果沒(méi)有, 需要使用函數(shù)的原型聲明
// 函數(shù)的原型聲明, 先聲明, 后實(shí)現(xiàn)
// 沒(méi)有函數(shù)體.只有函數(shù)名字和參數(shù)
void swap(int, int);
int main() {
// 交換兩個(gè)變量的值
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函數(shù)后面聲明
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;
}
-
引用傳遞
引用傳遞: 如果改變參數(shù)的值, 會(huì)對(duì)原來(lái)的值產(chǎn)生影響
#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;
}
// 引用傳遞值, 如果改變參數(shù)的值, 會(huì)對(duì)原來(lái)的值產(chǎn)生影響
// &取地址符號(hào)
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;
}