vc++编程:宏定义
Function prototype and function definition in C++ programming come into play while working with user-defined functions. User-defined functions are functions defined by the user as per the requirement of the program or to solve a given set of queries.
使用用户定义的函数时,C ++编程中的函数原型和函数定义会发挥作用。 用户定义的函数是用户根据程序的要求或解决一组给定查询而定义的函数。
There are two major parts of a user-defined function: 1) the function header which contains the Return type, Name and Parameter list of a function and 2) the function body that contains the set of statements or operations required needed to be carried out during the call of the function. The function header and the body of the function together are called as the function definition.
用户定义的函数有两个主要部分:1) 函数标头 ,其中包含函数的返回类型,名称和参数列表; 2) 函数主体 ,包含需要执行的一组语句或操作在函数调用期间。 函数标题和函数主体一起称为函数定义 。
Example of function definition:
函数定义示例:
void userdef()
{
cout <<"User defined function \n";
}
When a function is defined after the main function the compiler fails to recognize the function during its call when the program is being compiled, an error is displayed.
当在主函数之后定义函数时,编译器在编译程序时在调用期间无法识别该函数,则会显示错误。
To prevent this a function prototype is defined before the main function. A function prototype contains only the function header.
为了避免这种情况,在主要功能之前定义了功能原型 。 函数原型仅包含函数头 。
void userdef();
//or
void userdef(void);
C ++程序显示函数定义和原型 (C++ program to show function definition and prototype)
//C++ program to demonstrate example of
//function prototype and function definition
//(adding two numbers using user define function)
#include <iostream>
using namespace std;
//Function Prototype
int sum(int a, int b);
//main function
int main()
{
int x, y;
int result;
cout << "Enter two numbers: ";
cin >> x >> y;
result = sum(x, y);
cout << "Sum of numbers is: " << result << "\n";
return 0;
}
//Function Definition
int sum(int a, int b)
{
int z;
z = a + b;
return z;
}
Output
输出量
Enter two numbers: 36 24
Sum of numbers is: 60
vc++编程:宏定义