6.5函数的常见样式
摘自:哔哩哔哩黑马程序员
常见的函数样式有4种
1、无参无返
2、有参无返
3、无参有返
4、有参有返
例子:
#include<iostream>
using namespace std;
//无参无返
void test01()
{
cout<<"this is a test"<<endl;
}
//有参无返
void text02(int a){
cout<<"this is a test02"<<endl;
cout<<"a="<<a<<endl;
}
//无参有返
int text03()
{
cout<<"this is a test"<<endl;
return 10;
}
//有参有返
int text04(int b)
{
cout<<"this is a test"<<endl;
cout<<"b="<<b<<endl;
return b;
}
int main(){
test01();
text02 (100);
int num1=text03();
cout<<num1<<endl;
text04(1);
return 0;
}
总结:其实四种形式目的相同,只是最后再主main函数中所调用时不同罢了

浙公网安备 33010602011771号