关键差异 - 函数原型与函数在C中的定义
一个functionis a group of statements used to perform a specific task. InC programming,,,,the execution starts from main (). It is a function. Rather than writing all statements in the same program, it can be divided into multiple functions. Each function will perform different functionalities. The function prototype tells thecompilerabout the function name, return types andparameters。它也称为函数声明。每个功能都有一个特定的名称来识别它。该功能语句写在一对卷发括号中。功能可以返回值。有些功能不会返回值。数据使用参数列表传递到该函数。功能定义具有由函数执行的实际功能。在C编程中,有功能原型和功能定义。这key differencebetween the function prototype and function definition is that the函数原型仅包含函数的声明,而函数定义包含函数的实际实现。这function definition has thelocal variablesand the statements that determine what the function does.
CONTENTS
1。Overview and Key Difference
2。What is Function Prototype in C
3。什么是C中的函数定义
4.Similarities Between Function Prototype and Function Definition in C
5.并排比较 - 函数原型与函数在c中的函数定义以表格形式定义
6.Summary
C中的功能原型是什么?
功能原型提供函数声明。它指定功能的名称,返回类型,参数。返回类型是从函数返回的数据类型。当功能返回整数时,返回类型为int。当功能返回浮点值时,返回类型为浮点。如果该函数没有返回任何值,则是一个无效函数。功能名称用于识别它。C关键字不能用作函数名称。数据使用参数传递给该函数。该功能原型不包含该函数的实际实现。 The function prototype has the following syntax.
如果有一个函数来计算两个数字的最大值,则声明可以写为int max(int num1,int num2);最大值应在NUM1和NUM2中找到。这些是整数,它们传递给了功能。一开始,返回类型也是INT。因此,该函数返回整数值。不必在函数原型中编写参数名称。但是有必要编写数据类型。因此,int max(int,int);也是有效的功能原型。如果有两个整数为num1,num2,num3,并且原型写为int max(int num1,int num2,num3); it is invalid. The num1, num2 have the data types, but num3 does not have a data type. Therefore, it is invalid.
Refer the below program.
#include
int CarMax(int x, int y);
int main(){
int p =10;
int q = 20;
int答案;
answer = calMax(p,q);
printf(“The maximum value is %d\n”, answer);
返回0;
}
int calmax(int p,int q){
int值;
如果(p> q){
value = p;
}
else {
value = q;
}
返回值;
}
根据上述内容,第二个语句显示了函数原型。它没有实现。实际实施是在主要程序之后。当在一个源文件中定义函数并在另一个文件中的另一个源中调用该函数时,该功能原型更有用。
什么是C中的函数定义?
功能定义具有该函数的实际实现。它包含该功能应该做什么。当程序调用该函数时,控件会传输到调用函数。执行函数后,控件返回到主函数。所需的数据作为参数列表传递给该函数。如果有一个值返回,则提到返回类型。如果没有返回值,则返回类型是无效的。请参阅下面的功能以计算三角形的面积。
#include
Float calarea(int x,int y);
int main () {
int p =10;
int q = 20;
flaot area;
area = calArea(p,q);
printf (“The maximum value is %f\n”, area);
返回0;
}
float calarea(int x,int y){
float value;
value = 0.5 * x * y;
返回值;
}
一个ccording to the above program, the second statement indicates the function prototype. The actual implementation of what the function performs is written after the main program. It is the function definition. The p and q values are passed to the calArea function. The variable value is a local variable to the calArea function. The area is calculated and assigned to the variable value. Then it is returned back to the main program.
What are the Similarities Between Function Prototype and Function Definition in C?
- Both function prototype and function definition are related to functions.
- Both function prototype and function definition contain function name.
- Both function prototype and function definition contain return types.
- Both function prototype and function definition contain parameters.
C中的功能原型和功能定义有什么区别?
函数原型与C函数在C中的定义 |
|
这function prototype specifies the function name, return type, parameters but omits the function body. | 这function definition specifies the function name, return type; parameters include a function body. |
执行 | |
这function prototype does not have the function implementation. | 这function definition has the function implementation. |
概括 -函数原型与C函数在C中的定义
Using functions in programs has advantages. Functions increase code reusability. It is not necessary to write the same code again and again. Instead, the programmer can divide the program and call the necessary function. In C there are library functions. These functions are declared in the Cheader files。其中一些是printf(),scanf()等。程序员还可以编写自己的功能。与C中的功能相关的两个术语。它们功能原型和功能定义。C中函数原型和函数定义之间的区别在于,该函数原型仅包含函数的声明,而函数定义包含函数的实际实现。
在C中下载函数原型与函数定义的PDF
You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here:之间的区别Function Prototype and Function Definition in C
参考:
1.C功能定义。在这里可用
2。tutorialspoint.com. “C Functions.”重点。在这里可用
图片提供:
1.’c编程语言徽标by rezonansowy(公共领域)通过Commons Wikimedia
Leave a Reply