c++当要重复运行一些代码时可以打包一个函数

当没有返回值时用void打包函数:

#include <iostream>
#include <stdio.h>
using namespace std;
void xiaocai(){
    cout<<"xiaocai";
}
int main()
{
    xiaocai();
}

运行如下:

c++ 打包函数教程

 当一个函数要返回值的时候要用返回型的函数:

#include <iostream>
#include <stdio.h>
using namespace std;
int xiaocai(int a){
    return a;//返回值a 
}
int main()
{
    cout<<xiaocai(5);
}

运行如下:

c++ 打包函数教程

 欣赏代码:

输出绝对值:

#include <iostream>
#include <stdio.h>
using namespace std;
int xiaocai(int a){
    if(a>=0)
    return a;
    else
    return -a;
}
int main()
{
    cout<<xiaocai(-5)<<endl;
    cout<<xiaocai(6);
}

运行如下:

c++ 打包函数教程

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
  • 2021-11-27
  • 2022-01-19
  • 2022-12-23
  • 2021-11-28
猜你喜欢
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案