search
HomeCommon ProblemHow to understand function declaration in C language
How to understand function declaration in C languageMar 06, 2019 pm 02:31 PM
function declaration

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:

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

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

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

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;
}

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!

Statement
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
C++ 函数声明中的默认参数:全面解析其声明和用法C++ 函数声明中的默认参数:全面解析其声明和用法May 02, 2024 pm 03:09 PM

C++中的默认参数提供对函数参数指定默认值的功能,从而增强代码可读性、简洁性和灵活性。声明默认参数:在函数声明中将参数后加上"="符号,后跟默认值。用法:函数调用时,若未提供可选参数,则会使用默认值。实战案例:计算两个数之和的函数,一个参数必填,另一个可填并有默认值0。优点:增强可读性、增加灵活性、减少样板代码。注意事项:只能在声明中指定,必须位于末尾,类型必须兼容。

C++ 函数的声明和定义的顺序有什么影响?C++ 函数的声明和定义的顺序有什么影响?Apr 19, 2024 pm 01:42 PM

在C++中,函数声明和定义的顺序影响编译和链接过程。最常见的是声明在前,定义在后;也可使用“forwarddeclaration”将定义放在声明前;如果两者同时存在,编译器将忽略声明,仅使用定义。

C++ 函数声明和定义有什么区别?C++ 函数声明和定义有什么区别?Apr 18, 2024 pm 04:03 PM

函数声明告知编译器函数的存在,不包含实现,用于类型检查。函数定义提供实际实现,包含函数体。区分的关键特征包括:目的、位置、作用。理解差异对于编写有效且可维护的C++代码至关重要。

C++ 函数的声明和定义C++ 函数的声明和定义Apr 11, 2024 pm 01:27 PM

函数声明和定义在C++中是必要的,函数声明指定函数的返回类型、名称和参数,而函数定义包含函数体和实现。首先声明函数,然后在程序中使用它并传递所需的参数。使用return语句从函数中返回一个值。

C++编译错误:函数调用与函数声明不符,应该怎样解决?C++编译错误:函数调用与函数声明不符,应该怎样解决?Aug 22, 2023 pm 12:39 PM

C++编译错误:函数调用与函数声明不符,应该怎样解决?在开发C++程序时,难免会遇到一些编译错误,其中之一常见的错误是函数调用与函数声明不符的错误。这种错误广泛存在于C++程序员中,由于不注意函数声明的正确性,导致编译问题,最终浪费时间和精力修复问题,影响开发效率。避免这种错误的方法需要遵循一些规范和标准实践,下面让我们来了解一下。什么是函数调用与函数声明不

C++ 函数声明中的[[nodiscard]]:揭开忽略返回值后果的神秘面纱C++ 函数声明中的[[nodiscard]]:揭开忽略返回值后果的神秘面纱May 01, 2024 pm 06:18 PM

[[nodiscard]]属性指示函数的返回值不得忽略,否则将导致编译器警告或错误,以防止以下后果:未初始化异常、内存泄漏和错误的计算结果。

C++ 函数声明的详细语法:从语法解析到规范用法解析C++ 函数声明的详细语法:从语法解析到规范用法解析Apr 30, 2024 pm 02:54 PM

C++函数声明语法为:returnTypefunctionName(parameterType1parameterName1,...,parameterTypeNparameterNameN);,其中returnType为返回类型,functionName为函数名,parameterType为参数类型,parameterName为参数名,必须以分号结尾。

C++ 函数声明的逐步指南:涵盖每个步骤的详细说明C++ 函数声明的逐步指南:涵盖每个步骤的详细说明May 02, 2024 pm 04:33 PM

函数声明告诉编译器函数的存在,无需提供函数体。步骤如下:指定函数返回类型(void如果无返回值)定义函数名声明函数参数(可选,包括数据类型和标识符)加分号

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),