C语言及程序设计提高例程-8 函数的声明、定义和调用

来源:互联网 发布:数控编程入门先学什么 编辑:程序博客网 时间:2024/05/13 08:15

贺老师教学链接  C语言及程序设计提高 本课讲解


调用的条件——被调用的函数已存在

#include <stdio.h>#include <math.h>float max(float x, float y);int main (){    float a,b,c,s;    scanf("%f %f", &a, &b);    s=sqrt(a);    printf("sqrt is %.2f\n", s);    c=max(a+b, a*b) ;    printf("max is %.f\n", c);    return 0;}float max(float x, float y){    float z;    z=(x>y)? x : y ;    return  z;}


自定义函数先定义,后调用
#include <stdio.h>float max(float x, float y){    float z;    z=(x>y)? x : y ;    return  z;}
int main ()
{ float a,b,c; scanf("%f %f", &a, &b); c=max(a+b, a*b) ; printf("max is %.f\n", c); return 0;}


若要先调用,后定义:调用前声明
#include <stdio.h>float max(float, float);int main (){    float a,b,c;    scanf("%f %f", &a, &b);    c=max(a+b, a*b) ;    printf("max is %.f\n", c);    return 0;}float max(float x, float y){    float z;    z=(x>y)? x : y ;    return  z;}


编码规范:函数定义前的注释
/*功能:求出并返回两数的最大公约数参数:要求最大公约数的两个整数,均为整型返回值:两数的最大公约数算法:辗转相除法*/int gcd(int n1, int n2){    int r;    while(n2!=0)    {        r=n1%n2;        n1=n2;        n2=r;    }    return n1;}

0 0
原创粉丝点击