函数的常见样式
常见的函数样式分为 4 种
-
无参无返
void fun1()//不用传入参数 { cout << "wssb" << endl; }
int main() { //无参无返的函数调用: fun1(); system("pause"); return 0; }
运行结果:(环境:Windows11 (arm/Apple M VM)/Visual Studio 2022/Debug/arm64)
wssb 请按任意键继续. . .
-
有参无返
void fun2(int a) { cout << a << endl; cout << "wssb2" << endl; }
int main() { //有参无返的函数调用: fun2(100); system("pause"); return 0; }
运行结果:(环境:Windows11 (arm/Apple M VM)/Visual Studio 2022/Debug/arm64)
100 wssb2 请按任意键继续. . .
-
无参有返
int fun3() { cout << "wssb3" << endl; return 100; }
int main() { //无参有返的函数调用: int qwq = fun3(); cout << qwq << endl; system("pause"); return 0; }
运行结果:(环境:Windows11 (arm/Apple M VM)/Visual Studio 2022/Debug/arm64)
wssb3 100 请按任意键继续. . .
-
有参有返
int fun4(int a) { a = a + 100; return a; }
int main() { //有参有返的函数调用: int qwq = fun4(10); cout << qwq << endl; system("pause"); return 0; }
运行结果:(环境:Windows11 (arm/Apple M VM)/Visual Studio 2022/Debug/arm64)
110 请按任意键继续. . .
:-)