C++ 是C++;函数声明是可选的吗?

C++ 是C++;函数声明是可选的吗?,c++,C++,我只是在创建函数时注意到了一些东西。在守则中: #include <iostream> using namespace std; int add(int a, int b = 20) { int r; r = a + b; return (r); } int main() { int result; result = add(20); cout<<result; return 0; } 编译器会告诉我找不到标

我只是在创建函数时注意到了一些东西。在守则中:

#include <iostream>
using namespace std;

int add(int a, int b = 20)
{
    int r;
    r = a + b;
    return (r);
}

int main()
{
    int result;
    result = add(20);
    cout<<result;

    return 0;
}
编译器会告诉我找不到标识符
add()
。 那我们为什么还要声明函数呢?像这样:

#include <iostream>
using namespace std;

int add(int a, int b = 20);

int main()
{
    int result;
    result = add(20);
    cout<<result;

    return 0;
}

int add(int a, int b)
{
    int r;
    r = a + b;
    return (r);
}
#包括
使用名称空间std;
整数相加(整数a,整数b=20);
int main()
{
int结果;
结果=添加(20);

cout定义隐含着声明。声明必须在使用之前进行。

所有函数在使用之前都需要声明

您可以通过(1)编写声明或(2)编写定义来实现这一点

仅仅依靠(2)是很诱人的,但是你必须以一种特定的方式对你的程序进行排序,有时这是不可能的

   //void bar(int);

    void foo(int n)
    {
        if (!n){
            bar(n);
        }
    }

    void bar(int n)
        if (n){
            foo(n);
        }
    }

    int main()
    {
        foo(1);
    }


如果函数定义出现在函数调用之前,则原型不是必需的。否则,函数原型对于让编译器知道如何在调用函数时响应函数是必需的。

函数声明告诉编译器函数的名称、返回类型和参数。函数定义提供actu函数的所有主体

如果函数定义出现在函数调用之后,则原型是必需的。因为它告诉编译器调用函数时如何响应。

检查以下示例

/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int add(int, int);         // function prototype

void main()
{
    clrscr();
    int a, b;
    cout<<"Enter any two number: ";
    cin>>a>>b;
    cout<<"\nSummation = "<<add(a, b);
    getch();
}

int add(int x, int y)     // function definition
{
    int res;
    res = x + y;
    return res;
}
<代码> /*C++函数原型和C++函数定义*/ #包括 #包括 #包括 int add(int,int);//函数原型 void main() { clrsc(); INTA,b; couta>>b;
coutA定义是一个隐式声明。声明必须在使用之前出现。@YvesDaoust-这是一个答案。它属于下面的答案部分,可以在那里投票。就像从Kernighan和Ritchie开始的那样。它的设计目的是让编写单通道编译器更容易;-)@YvesDaoust:请不要请在回答部分回答问题。@ JohnSmith:当编译器第一次遇到标识符时,如果没有给出更多的细节,它就不能猜测它是什么。在旧的C中,有默认的假设允许编译器提供丢失的声明。这是不好的实践,在C++中是不可能的。@ JohnSmith:THA。差不多是这样,是的。
foo
需要低于
bar
bar
也需要低于
foo
。这是一个问题。功能一直在下降!@StoryTeller:偶尔会出现海龟。当然,这是我打算倡导的新设计原则的一个主要部分:“面向海龟的编程”@StoryTeller:你可以基于上世纪90年代的喜悦:实际上,定义是一种声明,但并非所有声明都是定义。
/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int add(int, int);         // function prototype

void main()
{
    clrscr();
    int a, b;
    cout<<"Enter any two number: ";
    cin>>a>>b;
    cout<<"\nSummation = "<<add(a, b);
    getch();
}

int add(int x, int y)     // function definition
{
    int res;
    res = x + y;
    return res;
}
/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int add(int x, int y)     // function definition
{
    int res;
    res = x + y;
    return res;
}

void main()
{
    clrscr();
    int a, b;
    cout<<"Enter any two number: ";
    cin>>a>>b;
    cout<<"\nSummation = "<<add(a, b);
    getch();
}