这条线的目的是什么? (函数声明) [英] What is the purpose of this line? (Function declaration)

查看:109
本文介绍了这条线的目的是什么? (函数声明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过K& R学习编程.到目前为止进展顺利,但是我不清楚第1.8节(函数)中的一行代码的作用.

I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions).

在1.8节中,作者向您展示了如何创建一个函数以将一个整数提高为另一整数的幂.

In section 1.8, the authors show you how to create a function to raise one integer to the power of another integer.

我粘贴了下面的代码,就像书中写的一样.一切输出正常.但是我不知道为什么他们在顶部添加了这一行:

I've pasted the code below, as it was written in the book. Everything outputs fine. But I don't know why they've included this line at the top:

int power(int m, int n);

书中没有提到它,除了说程序会将整数m乘幂n.如果我从代码中删除该行,该程序仍将按原样输出.

The book doesn't mention it, apart from saying that the program will raise integer m to the power n. If I remove the line from the code, the program still outputs as it should.

如果我正确理解,则此行

If I understand this correctly, the line

int power(int base, int n)

创建函数,下面的花括号定义函数.然后main下的花括号调用该函数以输出图表.

creates the function, and the braces underneath define the function. Then the braces under main call the function to output the chart.

所有这些似乎都有意义.但我看不出最重要的内容是什么.

So all that seems to make sense. But I don't see what the very top line does.

这可能是无关紧要的,但似乎我遗漏了一些东西.谁能启发我为什么要出现那条线?

It could be extraneous, but it seems far more likely that I'm missing something. Can anyone enlighten me as to why that line is there?

#include <stdio.h>

int power(int m, int n);

/* test power function */
main()
{
int i;

    for (i = 0; i < 10; ++i)
        printf("%d %d %d\n", i, power(2,i), power(-3, i));
    return 0;
}

/* power: raise base to n-th power; n >= 0 */

int power(int base, int n)
{
    int i, p;

    p = 1;
    for (i = 1; i <= n; ++i)
        p = p * base;
    return p;
}

推荐答案

第一行是函数的声明.底部的代码块是函数的定义.

The first line is the declaration of the function. The block of code at the bottom is the definition of the function.

从1999年版的ISO C标准开始,在没有可见声明的情况下调用函数是非法的(违反约束).声明必须在调用之前.

Starting with the 1999 version of the ISO C standard, it's illegal (a constraint violation) to call a function without a visible declaration; the declaration must precede the call.

对于像这样的简单程序,您可以只在main()定义之前编写power()的完整定义(因为定义还提供了声明),但是对于更复杂的情况(例如递归调用)您通常需要提供一个单独的声明.

For a simple program like this one, you could just write the full definition of power() before the definition of main() (since a definition also provides a declaration), but for more complex cases (such as recursive calls) you often need to provide a separate declaration.

对于大型程序,通常将所有函数声明收集在头文件(例如foo.h)中,并将相应的定义收集在源文件中(例如foo.c) ). #include "foo.h"指令用于使声明在其他文件中可见.您将在本书的后面看到这种事情.

For larger programs, it's common to collect all the function declarations in a header file (foo.h, for example), and the corresponding definitions in a source file (foo.c, for example). A #include "foo.h" directive is used to make the declarations visible in other files. You'll see that kind of thing later in the book.

(在K& R2所涵盖的1990和更早版本的C中,在某些情况下您可以在没有可见声明的情况下调用函数-但无论如何仍然要提供显式声明仍然是一个好主意.)

(In the 1990 and earlier versions of C, which is what K&R2 covers, there are cases where you can call a function without a visible declaration -- but it's still a very good idea to provide explicit declarations anyway.)

顺便说一句,主程序的声明实际上应该是int main(void)而不是main().

Incidentally, the declaration of the main program should really be int main(void) rather than just main().

术语:原型"是一个函数声明,用于指定参数的类型.

Terminology: a "prototype" is a function declaration that specifies the types of the parameters.

int power(int base, int n);    /* a declaration that's also a prototype */
int power(int, int);           /* likewise */
int power();                   /* a declaration but not a prototype */

(参数名称在定义中是必需的,但在独立声明中是可选的.)

(Parameter names are required in a definition, but optional in a standalone declaration.)

在特殊情况下,没有参数的函数原型使用(void),因为空括号已经意味着非原型声明.所以int main(void)是原型,但int main()不是.

As a special case, a prototype for a function with no parameters uses (void), since empty parentheses already mean a non-prototype declaration. So int main(void) is a prototype, but int main() isn't.

非原型声明是过时的",这意味着从理论上讲,它们可以从将来的语言标准中删除.但是自1989年以来,它们就已经过时了,甚至在新的2011 ISO C标准中,委员会也认为不适合删除它们.

Non-prototype declarations are "obsolescent", meaning that they could in theory be removed from a future language standard. But they've been obsolescent since 1989, and even in the new 2011 ISO C standard the committee hasn't seen fit to remove them.

这篇关于这条线的目的是什么? (函数声明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆