2.一般化结构:由一个main0函数和若干个其它 函数结合而成。 「案例1.2]由main0函数和1个max0函数构成的C语 言程序。 #include“stdio.h” #include“conio.h” void main() int numl,num2; printf("Input the first integer number:") scanf(%d",&numl); printf(Input the second integer number:") scanf“%d',&num2); printf(“max=%dn”,max(numl,num2): getchO); 2024/10/28 7
2024/10/28 7 2.一般化结构:由一个main()函数和若干个其它 函数结合而成。 [案例1.2] 由main()函数和1个max()函数构成的C语 言程序。 #include “stdio.h” #include “conio.h” void main( ) { int num1, num2; printf(“Input the first integer number: ”); scanf(“%d”, &num1); printf(“Input the second integer number: ”); scanf(“%d”, &num2); printf(“max = %d\n”, max(num1, num2)); getch(); }
int max(int x,inty) return(x>y x:y); 程序运行情况: Input the first integer number:6 Input the second integer number:9 max 9 2024/10/28 8
2024/10/28 8 int max( int x, int y) { return( x>y ? x : y ); } 程序运行情况: Input the first integer number: 6 ←┘ Input the second integer number: 9 ←┘ max = 9
[案例1.3]交换[案例1.2]中main()函数和max() 函数的位置。 源程序略。 程序运行情况: Input the first integer number:6 Input the second integer number:9 max 9 思考:[案例1.3]说明了什么? 2024/10/28
2024/10/28 9 [案例1.3] 交换[案例1.2]中main( )函数和max( ) 函数的位置。 源程序略。 程序运行情况: Input the first integer number: 6←┘ Input the second integer number: 9←┘ max = 9 思考:[案例1.3]说明了什么?
3.说明:函数是C语言程序的基本构成单位。 (1)mainO函数:C语言程序总是从main()函 数开始执行(不论其在程序中的位置),止于主函 数结束。 (2)其它函数:通过被main()函数直接或间接 调用而执行。 习惯:将主函数main(0放在最前头。 2024/10/28 10
2024/10/28 10 3.说明:函数是C语言程序的基本构成单位。 (1)main()函数: C语言程序总是从main( )函 数开始执行(不论其在程序中的位置),止于主函 数结束。 (2)其它函数:通过被main( )函数直接或间接 调用而执行。 习惯:将主函数main()放在最前头
1.2.2函数结构 任何函数(包括主函数main0)都是由函数说明和 函数体两部分组成: 「函数类型]函数名([函数参数表])} 函数说明 {说明语句部分; 执行语句部分; 函数体 2024/10/28 11
2024/10/28 11 1.2.2 函数结构 任何函数(包括主函数main())都是由函数说明和 函数体两部分组成: [函数类型] 函数名( [函数参数表] ) { 说明语句部分; 执行语句部分; } 函数说明 函数体