C语言中的函数

51 阅读1分钟

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

两个步骤: 1.先定义函数

    格式

返回值类型 函数名(参数列表){

  函数具体的代码
}
	(1)参数是可选的(可有可无的)
	(2)void 这个函数没有返回值
	

2.调用函数:

    格式
    
    返回值 = 函数名(参数)
    
    

实现特定功能的代码段

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

结果如下

image.png

将定义函数写在void中打印信息但不返回

例如

#include<stdio.h>

void printInfo(int age,double high){
    printf("--------------------\n");
    printf("年龄是:%d\n",age);
    printf("身高是:%f\n",high);
    printf("--------------------\n");
	
}
int main(){
	
    printInfo(18,1.78);
    return 0;
}


结果如下

image.png