keil5函数 默认返回值_C++ 函数的定义

 C++对于函数的基本用法

dc67954a02e5ada7f8d72e098fbfee2a.png

01

函数的定义

//函数声明:[返回值类型] [函数名称] (参数列表)int Function(int a, int b);//函数定义int Function(int a, int b){  //函数体  return a + b;}

02

函数的默认参数

定义函数时可以在参数列表中为形参指定默认值

int Function2(int a, int b = 10){  return a + b;}int main(){  Function2(10);}

03

函数的重载

函数的重载条件:

  1. 函数名称相同

  2. 函数作用域相同

  3. 函数的返回值相同

  4. 函数的参数列表不相同(数量不同、类型不同或顺序不同)

void Function3(int a){  cout << "Function3(int a)" << endl;}void Function3(int a, int b){  cout << "Function3(int a,int b)" << endl;}void Function3(int a,string b){  cout << "Function3(int a,string b)" << endl;}void Function3(string a, int b){  cout << "Function3(string a,int b)" << endl;}int main(){  Function3(10);  Function3(10, 20);  Function3(10, "aa");  Function3("aa", 20);  system("pause");}

输出结果:

c174583ce0dca728661eb7844aabe0d1.png

04

引用作为参数的重载

//引用做为重载条件void Function4(int &a){  a += 100;  cout << "Function4(int &a)" << endl;}//Const做为形参的修饰符,可以实现重载void Function4(const int &a){  cout << "Function4(const int &a)" << endl;}int main(){  int a = 10;  Function4(a);//a为可修改参数  cout << a << endl;//调用后a的值变为110  int *pa = &a;  Function4(*pa);//非静态指针依然可以允许修改值  cout << a << endl;//调用后值为210  const int b = 100;//const修饰的变量不可修改    Function4(b);//调用const修饰参数的重载函数  const int *pb = &b;//const指针指向const修饰的变量b  Function4(*pb);//调用const修饰参数的重载函数  Function4(20);//依旧调用const修饰参数的重载函数  system("pause");}

函数输出结果:

6f26ceeb0f6786083870687682a3fa71.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值