函数

27 阅读1分钟

函数:实现特定功能的代码段

我给它一些什么,它给我做什么事,还给我什么。

两个步骤:

先定义函数:格式返回值类型 函数名 (参数列表)

{

函数具体的代码

}

(1)参数是可选的(可有可无的)

(2)void 这个函数没有返回值

调用函数:

格式

返回值 = 函数名 (参数)

实现特定功能的代码

年龄

#include 

void  printAge() {
		int age =18;
		printf("---------------------------\n");
		printf("年龄是:%d \n",age);
		printf("---------------------------\n");
}


int main(){
    //函数
printAge();
}

image.png

年龄和身高

#include 
void  printAge(int age,double prm) {
		printf("---------------------------\n");
		printf("年龄是:%d \n",age);
		printf("身高是:%f \n", prm);
		printf("---------------------------\n");
}


int main(){
    //函数
printAge(189,1.80); //调用函数
printAge(19,1.90);
 
}

image.png