1.常见的函数样式有4种
1.无参返回
#include<iostream>
using namespace std;
void test01()
{
cout<<"this is test01"<<endl;
}
//调用:
int main()
{ //无参调用
test01;
}
2.有参返回
#include<iostream>
using namespace std;
void test02(int a)
{
cout<<"this is test02 a="<<a<<endl;
}
//调用:
int main()
{ //有参无返调用
test02(100);
}
3.无参返回
#include<iostream>
using namespace std;
int test03()
{
cout<<"this is test03"<endl;
return 1000;
}
//调用:
int main()
{ //无参有返调用
int num1=test03();//num1是为了接受返会结果
cout<<"num1="<<num1<<endl;
}//执行时num1会被赋与1000
}
4.有参返回
#include<iostream>
using namespace std;
int test04(int a)
{
cout<<"this is test04 a="<<a<<endl;
return a;
}
//调用:
int main()
{ //有参无有返调用
int num2=test04(10000);
cout<<"num2="<<num2<<endl;
}
2.函数的声明
作用:告诉编译器函数名称如何调用函数,等同于提前告诉编译器函数的存在,函数的实际主体可以单独定义,并且函数的声明可以多次,但函数的定义只能有一次
函数声明做法: 函数类型 函数名(形参);
如果将函数放在main函数下面,编译器会不知道该函数,会出现报错的情况,因为编译顺序是上往下,所以如果想写在mian函数下面,得提前声明.
仅个人笔记与理解,如有误可指出,谢谢