Home > Common Problem > How to understand function declaration in C language

How to understand function declaration in C language

清浅
Release: 2020-09-10 13:16:12
Original
29865 people have browsed it

The format of C language function declaration is composed of removing the function body in the function definition and adding a semicolon. The purpose is to prevent the program from using the function before the function is undefined, causing the program to report an error.

How to understand function declaration in C language

The C language code is executed from top to bottom. In principle, the function definition must appear before the function call, otherwise an error will be reported. But in actual development, they are often used before the function is defined. At this time, they need to be declared in advance. Next, in the article, we will introduce the knowledge about function declaration in detail, which will have a certain reference effect. I hope it will be helpful to everyone.

[Recommended course: C Language Tutorial]

Function declaration

So-called Declaration is to tell the compiler that I am going to use this function. It doesn't matter if its definition is not found now, but please don't report an error and the definition will be added later.
The format of the function declaration is very simple, which is equivalent to removing the function body in the function definition and adding a semicolon;, as shown below:

返回值类型  函数名( 类型 形参, 类型 形参… );
Copy after login

You can also write no formal parameters, only the data type:

返回值类型  函数名( 类型, 类型…);
Copy after login

The function declaration gives the function name, return value type, parameter list (parameter type) and other information related to the function, which is called the function prototype (Function Prototype).
The function prototype is to tell the compiler information related to the function, so that the compiler knows the existence of the function and its existing form. Even if the function is not defined temporarily, the compiler knows how to use it.
With function declaration, function definition can appear anywhere, even in other files, static link libraries, dynamic link libraries, etc.

Example:

#include <stdio.h>
// 函数声明
long factorial(int n); 
//也可以写作 long factorial(int);
long sum(long n); //也可以写作 long sum(long);
int main(){
printf("1!+2!+...+9!+10! = %ld\n", sum(10));return 0;
}
//求阶乘
long factorial(int n)
{
int i;
long result=1;
for(i=1; i<=n; i++){
result *= i;
}
return result;
}
// 求累加的和
long sum(long n)
{int i;
long result = 0;
for(i=1; i<=n; i++)
{
//嵌套调用
result += factorial(i);
}
return result;
}
Copy after login

The running result is: 1! 2! ... 9! 10! = 4037913

We know that using printf( ), puts(), scanf(), getchar() and other functions must introduce the header file stdio.h. Many beginners think that stdio.h contains the function definition (that is, the function body). As long as there is a header file, the program will Can run. In fact, it is not the case. The header file contains function declarations, not function definitions. The function definitions are all in the system library. If there is only a header file without a system library, an error will be reported during linking, and the program will not run at all.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of How to understand function declaration in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template