vc++编程:宏定义_C ++编程中的功能协议和功能定义

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.

当在主函数之后定义函数时,编译器在编译程序时在调用期间无法识别该函数,则会显示错误。

ERROR: Function protocol and function definition in C++

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


翻译自: https://www.includehelp.com/cpp-tutorial/function-protocol-and-function-definition-in-cpp-programming.aspx

vc++编程:宏定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值