文档详情

C语言_05次课-2-2.pdf

发布:2017-09-27约1.25万字共33页下载文档
文本预览下载声明
第六讲 函数 6.1 概述 6.2 函数的定义 6.3 函数的返回值 6.4 函数的调用 6.5 函数的递归调用 6.1 概述 编程中常遇到完成某个功能的程序段出 现多次;大家均要用到的功能。 为了减少不必要的重复编程使程序质量 提高。在计算机高级语言中,引入函数 (或子程序、过程)。 6.1 概述 C程序是由一个主函数和其它若干函数构 成,每个函数实现一定的功能,其中主函 数main()是必需的,其它函数被主函数调 用或者其它函数之间相互调用。C语言的函 数可以分为三类:主函数main()、库函数 (如printf()、scanf()等)和用户自定义 函数。 // 程序L1_3.C :输入a、b两个数,输出其中最大数 #include stdio.h /* 包含输入输出头文件 */ float max(float x, float y) /* 定义求x、y 的最大值的函数max*/ {float z; /* 定义实数变量z*/ if (xy) z=x; /* 如果x大于y,则z等于x */ else z=y; /* 否则z 等于y */ return z; /* 返回z, 则max函数值为z的值 */ } main( ) /* 定义主函数 */ {float a,b,c; /* 定义实数变量a、b、c */ printf(Please input two numbers (a,b):); scanf(%f,%f,a,b); /* 从键盘输入a、b */ c=max(a,b); /* c等于a、b的最大数 */ printf(%f,%f,the max is %f\n,a,b,c); } } /* 程序L5_11.C功能:输入年、月、日,输出该日期是该年的第几天 */ #include stdio.h /*函数is_leap_year()判断年year是否是闰年,如果是返回值为1,否 则为0*/ int is_leap_year(int year) { int leap; if (year%4==0year%100!=0||year%400==0) leap=1; else leap=0; return leap; } /*函数len_of_month()的返回值为某年year 的某月month的天数*/ int len_of_month(int year,int month) { int month_days; if (month==2) if (is_leap_year(year)) month_days=29; else month_days=28; else if (month==4||month==6||month==9||month==11) month_days=30; else month_days=31; return month_days;} /*函数len_of_days()的返回值为该日期date是该年year 的第几天*/ int len_of_days(int year,int month,int date) { int total_days,n; for(n=1,total_days=0;nmonth;n++) total_days+=len_of_month(year,n); total_days+=date; return total_days; } void main() { int year,month,date,days,n; printf(please input year,month,date:); scan
显示全部
相似文档