1.函數的默認參數
a.函數可以有默認參數,但是必須從右往左默認。
b.函數的聲明有默認參數,定義時候就不可有;定義時候有默認參數,聲明時候就沒有。
void jiafa(int a,int b=19, int c=29)//默認參數
{
return a+b+c;
}
2.函數的佔位參數
用法:
1.函數類型 函數名 (變量類型)
2.函數的佔位參數,也可以有默認值
3.佔位參數必須給值
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解決system不明確問題。
void qqq(int a,int ,int = 20)//函數的佔位參數,也可以有默認值
{
cout << "是函數" << endl;
}
int main()
{
qqq(2,3);//佔位參數必須給值
system("pause");
return 0;
}
3.函數重載
//1.在一個作用域下(此處均在全局區)
//2.函數同名
//3.依靠函數 參數類型不同,參數個數不同,或者順序不同加以區分
//4.不可以用返回值的不同來區分
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解決system不明確問題。
void func()
{
cout << "函數調用" << endl;
}
void func(int a)
{
cout << "函數(int a)的調用" << endl;
}
void func(int a, double b)
{
cout << "函數(int a, double b)調用" << endl;
}
void func(double a, int b)
{
cout << "函數(double a, int b)的調用" << endl;
}
//int func(double a, int b)
//{
// cout << "函數(double a, int b)的調用" << endl;
//}.不可以用返回值的不同來區分
int main()
{
//函數重載
//1.在一個作用域下(此處均在全局區)
//2.函數同名
//3.依靠函數 參數類型不同,參數個數不同,或者順序不同加以區分
//4.不可以用返回值的不同來區分
func(1.2, 1);
system("pause");
return 0;
}
4.函數重載注意事項
//1.引用作爲函數參數
2.函數重載遇到默認參數,無法識別時候,此種情況儘量避免
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解決system不明確問題。
//函數重載注意事項
//1.引用作爲函數參數
void func(int& a)
{
cout << "函數(int& a)的調用" << endl;
}
void func(const int& a)
{
cout << "函數(const int& a)的調用" << endl;
}
////2.函數重載遇到默認參數,此種情況儘量避免
void funcc(int a)
{
cout << "函數(int a)的調用" << endl;
}
void funcc(int a, int b = 10)
{
cout << "函數(int a, int b = 10)的調用" << endl;
}
int main()
{
int a = 10;
func(a);
func(10);
funcc(10,20);
system("pause");
return 0;
}