功能声明 - 为什么需要它们? [英] Function declarations - why are they needed?

查看:61
本文介绍了功能声明 - 为什么需要它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C的设计有疑问。为什么我们必须声明

函数?一旦调用一个函数,这是不可避免的,因为

编译器需要知道它的定义才能分支到它,所以用

定义它会知道返回类型和参数类型。为什么

我们是否需要在我们需要的每个源文件中声明函数?


例如在Python函数中定义一次并且从未声明

无处不在。

I have a question about the design of C. Why do we have to declare
functions? It''s inevitable that once a function is called the
compiler will need to know its definition to branch to it, so with
the definition it''ll know the return type and the argument types. Why
do we need to declare functions in every source file we need them?

For example in Python functions are defined once and never declared
anywhere.

推荐答案

Seeker写道:
Seeker wrote:

我有一个问题关于C.的设计我们为什么要声明

函数?一旦调用一个函数,这是不可避免的,因为

编译器需要知道它的定义才能分支到它,所以用

定义它会知道返回类型和参数类型。为什么

我们需要在我们需要的每个源文件中声明函数吗?
I have a question about the design of C. Why do we have to declare
functions? It''s inevitable that once a function is called the
compiler will need to know its definition to branch to it, so with
the definition it''ll know the return type and the argument types. Why
do we need to declare functions in every source file we need them?



我们不是,只要它们之前被定义它们被使用。

We don''t, provided they are defined before they are used.


例如在Python函数中定义一次并且从未在任何地方声明


For example in Python functions are defined once and never declared
anywhere.



Python是一种解释性语言,解释器可以在运行时查看所有

函数定义(或找到它们)。 C是一种编译的
语言,其中组件可以独立构建(编译),因此必须声明其他地方定义的
函数。

-

Ian Collins。

Python is an interpreted language where the interpreter can see all the
function definitions (or find them) at run time. C is a compiled
language where components can be built (compiled) in isolation, so
functions that are defined elsewhere have to be declared.

--
Ian Collins.


搜索者发布:
Seeker posted:

我对C的设计有疑问。为什么我们要声明

函数?一旦调用一个函数,这是不可避免的,因为

编译器需要知道它的定义才能分支到它,所以用

定义它会知道返回类型和参数类型。为什么

我们是否需要在我们需要的每个源文件中声明函数?


例如在Python函数中定义一次并且从未声明

无处不在。
I have a question about the design of C. Why do we have to declare
functions? It''s inevitable that once a function is called the
compiler will need to know its definition to branch to it, so with
the definition it''ll know the return type and the argument types. Why
do we need to declare functions in every source file we need them?

For example in Python functions are defined once and never declared
anywhere.



编译器读取源文件就像人类一样,逐行,左边

到右边,从上到下。因此,如果它读取以下源文件:


void Func(void)

{

int j = 7;


j + = OtherFunc(); / *这一行* /

}

int OtherFunc(无效)

{

返回3;

}


它将达到此线并说什么是OtherFunc?从来没有听说过

it!"


因此,您通过提供声明将其介绍给编译器:


int OtherFunc(void);


void Func(void)

{

int j = 7;


j + = OtherFunc();

}


int OtherFunc(void)

{

返回3;

}


因为我们可以在定义之前声明事物,所以可以使用

两件事要互相引用。考虑两个函数调用每个

其他:

int Func2(int const);


int Func1( int const i)

{

如果(7 == i)返回Func2(i);


返回i - 3 ;

}

int Func2(int const i)

{

返回Func1(i + 9);

}

-


Frederick Gotham


A compiler reads a source file just like a human would, line by line, left
to right, top to bottom. So if it reads the following source file:

void Func(void)
{
int j = 7;

j += OtherFunc(); /* THIS LINE */
}
int OtherFunc(void)
{
return 3;
}

It will get as far as THIS LINE and say "What is OtherFunc? Never heard of
it!".

Therefore you introduce it to the compiler by providing a declaration:

int OtherFunc(void);

void Func(void)
{
int j = 7;

j += OtherFunc();
}

int OtherFunc(void)
{
return 3;
}

Because we can declare things before defining things, it''s possible to have
two things to refer to each other. Consider two functions that call each
other:

int Func2(int const);

int Func1(int const i)
{
if (7 == i) return Func2(i);

return i - 3;
}
int Func2(int const i)
{
return Func1(i + 9);
}
--

Frederick Gotham




Seeker写道:

Seeker wrote:

我对C的设计有疑问。为什么我们要声明

函数?
I have a question about the design of C. Why do we have to declare
functions?



有几个原因。


Several reasons.


>一旦函数被调用,这是不可避免的

编译器需要知道它的定义才能分支到它,
>It''s inevitable that once a function is called the
compiler will need to know its definition to branch to it,



不完全。大多数C编译器都可以使用外部引用发出代码,

是由单独的链接器程序找到的
。因此,当你调用foo()时,

编译器会逐字地输入.obj文件操作码(调用),foo。

编译器不知道在哪里foo是源代码或目标代码。

Not quite. Most C compilers can emit code with external references,
which are
found by a separate linker program. So when you call foo(), the
compiler puts out literally into the .obj file opcode(call),"foo".
The compiler has no idea where foo is, source or object code.


所以

定义它会知道返回类型和参数类型。
so with
the definition it''ll know the return type and the argument types.



如前所述,foo()源文件和目标文件目前可能不存在


编译器肯定可以使用一些提示作为参数并返回

类型,否则它不知道是否要推送参数值,

或地址(如果是C ++ by-ref参数),它不知道

如何处理返回值。

As mentioned before, the foo() source and object files may not even
exist at this time.
The compiler sure can use some hint as to the parameters and return
types, other wise it won''t know whether to push the parameter values,
or addresses (in case of a C++ by-ref parameter), and it won''t know
what to do with the returned value.

这篇关于功能声明 - 为什么需要它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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