int y)int max(int X,A个23int z;z=x>y?x:y;return z;3#include <stdio.h>int mainOint a, b, c;printf("two integer nymbers:");版权所有@陈暂保留所有scanf("%d, %d", &a, &b);max(a, b) ;cprintf("max is %d)n", c);权利11
版权所有© 陈哲保留所有权利 int max(int x, int y ) { int z; z = x > y ? x : y; return z; } #include <stdio.h > int main() { int a, b, c; printf ("two integer numbers:"); scanf("%d, %d", &a, &b); c = max(a, b); printf ("max is %d \n", c); } 11 3 2 3
>例:将前一个例子改为比较两个float型数据,但仍然返回整型数据>编程思路:如果返回值的类型与函数类型不同,将进行隐式类型转化版权所有@陈哲保备所有权利12
版 权 所 有 © 陈 哲 保 留 所 有 权 利 ➢例:将前一个例子改为比较两个float型数 据,但仍然返回整型数据。 ➢编程思路:如果返回值的类型与函数类 型不同,将进行隐式类型转化。 12
#include <stdio.h>int max(float x, float y);int main1float a, b; int c;scanf("%f,%f",&a, &b) ;max(a, b);cprintf("max is %d)n",c)return 0; 1.52.6int max(float X, float y)权所有@陈哲保留所有权float z;z=x>y?x:y;return z;2利2.613
版权所有© 陈哲保留所有权利 #include <stdio.h > int max(float x, float y); int main() { float a, b; int c; scanf ("%f,%f ", &a, &b); c = max(a, b); printf ("max is %d \n", c); return 0; } int max(float x, float y ) { float z; z = x > y ? x : y; return z; } 13 1.5 2.6 2.6 2
函数调用的语义>在函数调用之前,形参并不占内存空间>在函数调用时,在内存中为函数分配内存空间,称为栈帧(stack frame),包括函数中定义的所有变量(包括形参)的内存空间;版权所有@陈暂保留所有权利zyxmax的栈帧main的栈帧23bac14
版 权 所 有 © 陈 哲 保 留 所 有 权 利 函数调用的语义 ➢在函数调用之前,形参并不占内存空间; ➢在函数调用时,在内存中为函数分配内 存空间,称为栈帧(stack frame),包括 函数中定义的所有变量(包括形参)的 内存空间; 14 a 2 b 3 x y main的栈帧 max的栈帧 c z
>把实参的值传递给被调用函数的形参(相当于为形参赋值):>执行函数体中的语句,将返回值记录在内存中;>最后释放在内存中为函数分配的内存空间,包括函数中定义的所有变量(包括形参)的内存空间。返回值版权所有@陈暂保留所有权利332zy3xmax的栈顿3main的栈帧23abc15
版 权 所 有 © 陈 哲 保 留 所 有 权 利 ➢把实参的值传递给被调用函数的形参 (相当于为形参赋值); ➢执行函数体中的语句,将返回值记录在 内存中; ➢最后释放在内存中为函数分配的内存空 间,包括函数中定义的所有变量(包括 形参)的内存空间。 15 a 2 b 3 x y main的栈帧 max的栈帧 c 2 3 z 3 3 返回值 3