Function prototype in C language (codeblocks compilation environment)

Question: What is the purpose of the function prototype?

#include<stdio.h>
//无函数原型, 程序编译时会报错.
int main()
{
    int x ,y;
    scanf("%d%d", &x, &y);
    printf("max=%d\n", get_Max(x, y));
    //
    return 0;
}
int get_Max(int a, int b)
{
    return a>=b ? a : b;
}

#include<stdio.h>
int get_Max(int x, int y);//函数原型
int main()
{
    int x ,y;
    scanf("%d%d", &x, &y);
    printf("max=%d\n", get_Max(x, y));
    //
    return 0;
}
int get_Max(int a, int b)
{
    return a>=b ? a : b;
}

    If the function P() is called in the function get_Max(), but the function definition of the function P() is located after the get_Max() function definition, then you only need to add the function prototype of the function P() before the get_Max() function definition. But. In addition, function prototypes do not have to be written at the beginning of the program. 

#include<stdio.h>
int get_Max(int t, int z);//函数原型
int main()
{
    int x ,y;
    scanf("%d%d", &x, &y);
    printf("max=%d\n", get_Max(x, y));
    //
    return 0;
}
void P();//函数原型
int get_Max(int a, int b)
{
    P();
    return a>=b ? a : b;
}
void P()
{
    printf("Welcome!\n");
    return ;
}

    It is a good programming practice to give the function prototypes of all functions at the beginning of the program.

#include<stdio.h>
int get_Max(int t, int z);
void P();//函数原型
int get_Max(int a, int b)
{
    P();
    return a>=b ? a : b;
}
void P()
{
    printf("Welcome!\n");
    return ;
}
int main()
{
    int x ,y;
    scanf("%d%d", &x, &y);
    printf("max=%d\n", get_Max(x, y));
    //
    return 0;
}

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42048463/article/details/115264685