有几个问题了。功能原型 [英] A couple of questions re. the function prototype

查看:77
本文介绍了有几个问题了。功能原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿嘿,


我在这里读了一个很好的主题,关于我们使用函数

原型的原因,它回答了我的大部分问题,但是我想要对b / b
进行双重检查,因为我正在写一些关于

函数的东西,而且我不喜欢写关于我不是的东西肯定的。


好​​的,那么,我们走了:


我最初认为只需要使用一个函数

原型如果函数返回的类型不是int - 我的印象是如果没有原型,编译器

会假设这是一个int。但是,如果我不使用原型,即使函数返回int,我的编译器(gcc)也会向我发出警告

。所以

这让我想知道我是否错误认为我没有必要使用原型来返回一个int函数。现在我认为

关于它,原型不仅允许编译器检查

一个返回类型吗?它还允许它检查函数调用的

参数是否与函数的参数

的类型相同? (是的,我对C来说还是比较新的)所以

的问题是:它是否合适(关于制作便携式的

程序)*总是*使用函数原型?然后我的第二个

问题是,如果实际函数也作为原型,为什么

不只是将所有函数放在main()之前?我不是想在这里开始发火焰BTW,如果这是潜在的火焰材料 - 我只是在寻找你的想法,因为我真的不知道关于这个问题很多

。谢谢


Rob Somers

Hey people,

I read a good thread on here regarding the reason why we use function
prototypes, and it answered most of my questions, but I wanted to
double check on a couple of things, as I am writing something up on
functions, and I don''t like writing about things I am not sure about.

Ok, then, here we go:

I initially thought that one would only really need to use a function
prototype if the type the function returned was not an int - I was
under the impression that if there was no prototype, the compiler
would assume it was an int. However, my compiler (gcc) squawks at me
if I do not use a prototype even if the function returns an int. So
this makes me wonder if I was wrong in thinking that I did not have to
use a prototype for a function returning an int. And now that I think
about it, the prototype does more than allow the compiler to check for
a return type perhaps? It also allows for it to check that the
arguments at the function call are of the same type as the parameters
of the function? (Yes I am still relatively new to C) So the
question is this: Is it proper (as regarding making portable
programs) to *always* use a function prototype? And then my second
question is, If the actual function serves as a prototype also, why
not just put all the functions before main()? I am not trying to
start flames here BTW, if this is potential flame material - I am
simply looking for your thoughts, as I really don''t know that much
about the subject. Thanks

Rob Somers

推荐答案

Rob Somers写道:
Rob Somers writes:
然后我的第二个问题是,如果实际功能也作为原型,为什么
不只是将所有功能放在main()之前?我不是想在这里开始火焰BTW,如果这是潜在的火焰材料 - 我只是在寻找你的想法,因为我真的不太了解这个主题。谢谢
And then my second
question is, If the actual function serves as a prototype also, why
not just put all the functions before main()? I am not trying to
start flames here BTW, if this is potential flame material - I am
simply looking for your thoughts, as I really don''t know that much
about the subject. Thanks




我这样编写和调试代码。但是当它完成,或者完成任何事情时,我将定义移到main的末尾并编写原型,我知道它们到底是什么而且他们中的大多数人都会这样。


为什么?这是一个文档辅助工具。如果你想在世界地图上的某个不起眼的国家寻找一个不起眼的小村庄

,那将很难找到。两张

地图比一张好。一个很好命名的功能列表和一点运气

你可能会对这个野兽的组织方式有所了解。



I do write and debug code that way. But when it is done, or as done as
anything ever gets, I move the definition to the end of main and write
prototypes, I know exactly what they are now and most of them are going to
stay that way.

Why? It''s a documentation aid. If you were to look for some obscure hamlet
in some obscure country on a map of the world it would be hard to find. Two
maps are better than one. A list of well named functions and a bit of luck
and you might be able to get some idea as to how this beast is organized.


On Sun,2003年11月9日09:25:26 -0800,Rob Somers写道:
On Sun, 09 Nov 2003 09:25:26 -0800, Rob Somers wrote:
嘿嘿,

我在这里读了一个很好的帖子我们之所以使用函数原型,并且它回答了我的大多数问题,但是我想对两件事情进行双重检查,因为我正在写一些关于
函数的东西,以及我不喜欢写关于我不确定的事情。

好的,那么,我们走了:

我的印象是如果没有原型,编译器会认为它是一个int。但是,我的编译器(gcc)向我发出警告


这种风格的C已经过时了10多年。如果您使用书籍建议这样做,您需要更新您的书籍。

实际上,省略函数的返回类型不再是合法的C,

所以要感谢gcc抱怨。

这让我想知道我是否错误认为我没有必要使用原型来返回一个函数INT。


正确,你错了。

现在我认为关于它,原型不仅仅是允许编译器检查<或许返回类型?它还允许它检查函数调用的
参数是否与函数的参数
类型相同?


绝对正确。

是否正确(如制作便携式程序)*总是*使用函数原型?


是的。

然后我的第二个问题是,如果实际功能也作为原型,为什么
不只是把所有的功能都放在main()之前?
Hey people,

I read a good thread on here regarding the reason why we use function
prototypes, and it answered most of my questions, but I wanted to
double check on a couple of things, as I am writing something up on
functions, and I don''t like writing about things I am not sure about.

Ok, then, here we go:

I was under the impression that if there was no prototype, the compiler
would assume it was an int. However, my compiler (gcc) squawks at me
This style of C has been outdated for more than 10 years. If you
are using books that suggest this, you need to update your books.
In fact, omitting the return type of a function is no longer legal C,
so be thankful that gcc is complaining.
this makes me wonder if I was wrong in thinking that I did not have to
use a prototype for a function returning an int.
Correct, you were wrong.
And now that I think
about it, the prototype does more than allow the compiler to check for
a return type perhaps? It also allows for it to check that the
arguments at the function call are of the same type as the parameters
of the function?
Absolutely correct.
Is it proper (as regarding making portable
programs) to *always* use a function prototype?
Yes it is.
And then my second
question is, If the actual function serves as a prototype also, why
not just put all the functions before main()?




我大部分时间做的都是这样,假设是通过all你的意思是

所有的功能碰巧都在同一个源文件中

main()"。


C程序通常是源文件的集合,每个源文件都包含一些函数定义。一些函数是

意味着要导出它们的原型包含在一个相关的

头文件中,以包含在其他源文件中。有些功能是

并不意味着要导出。它们是用关键字static

定义的,只要呼叫者来自源文件中的被叫者,那么

就不需要额外。原型。


-Sheldon



I do exactly that most of the time, assuming that by "all" you mean
"all of the functions that happen to be in the same source file as
main()".

C programs are usually a collection of source files, each of which
contains some number of function definitions. Some functions are
meant to be exported and their prototypes are included in an associated
header file for inclusion in other source files. Some functions are
not meant to be exported. They are defined with the keyword static
and as long as callers come after callees in the source file, there
is no need for an "extra" prototype.

-Sheldon


2003年11月9日09:25:26 -0800
ke*****@gto.net (Rob Somers)写道:
On 9 Nov 2003 09:25:26 -0800
ke*****@gto.net (Rob Somers) wrote:
嘿人,

我在这里读了一个关于我们使用功能原型的原因的好线程,它回答了我的大多数问题,但是我想要仔细检查关于一些事情,因为我正在写一些关于
功能的东西,而且我不喜欢写关于我不确定的事情。

好的,那么,我们走吧:

我最初认为,如果函数返回的类型不是int,那么只需要使用函数原型 - 我的印象是,如果有的话没有原型,编译器会认为它是一个int。


别忘了你可以做类似的事情:

float func();


没有提供原型(如果我记得我的定义

正确),因为它没有指定参数类型。


但是,通常是好的做法,以确保所有调用的范围都有一个

原型,因为它允许编译器确保参数是兼容类型的



但是,我的编译器(gcc)对我说话,如果我不使用原型,即使该函数返回一个int。所以这让我想知道我是否错误地认为我没有必要使用原型来返回一个返回int的函数。现在我认为关于它,原型不仅仅允许编译器检查返回类型吗?它还允许它检查函数调用的
参数是否与函数的参数
类型相同? (是的,我对C仍然相对较新)所以
问题是这样的:是否正确(如制作便携式程序)*总是*使用函数原型?


这是一个很好的做法,因为它可以帮助编译器为你找到错误。

任何能帮助编译器找到错误的东西都是个好主意。

然后我的第二个问题是,如果实际函数也作为原型,为什么
不只是将所有函数放在main()之前?我不是想在这里开始火焰BTW,如果这是潜在的火焰材料 - 我只是在寻找你的想法,因为我真的不太了解这个主题。谢谢
Hey people,

I read a good thread on here regarding the reason why we use function
prototypes, and it answered most of my questions, but I wanted to
double check on a couple of things, as I am writing something up on
functions, and I don''t like writing about things I am not sure about.

Ok, then, here we go:

I initially thought that one would only really need to use a function
prototype if the type the function returned was not an int - I was
under the impression that if there was no prototype, the compiler
would assume it was an int.
Don''t forget you can do something like:
float func();

which does not provide a prototype (if I remember my definitions
correctly) since it does not specify the parameter types.

However, it is generally good practise to ensure that there is a
prototype in scope for all calls since it allows the compiler to ensure
that the parameters are of compatible types.
However, my compiler (gcc) squawks at me
if I do not use a prototype even if the function returns an int. So
this makes me wonder if I was wrong in thinking that I did not have to
use a prototype for a function returning an int. And now that I think
about it, the prototype does more than allow the compiler to check for
a return type perhaps? It also allows for it to check that the
arguments at the function call are of the same type as the parameters
of the function? (Yes I am still relatively new to C) So the
question is this: Is it proper (as regarding making portable
programs) to *always* use a function prototype?
It is good practise since it helps the compiler to find errors for you.
Anything that helps the compiler find errors for you is a good idea.
And then my second
question is, If the actual function serves as a prototype also, why
not just put all the functions before main()? I am not trying to
start flames here BTW, if this is potential flame material - I am
simply looking for your thoughts, as I really don''t know that much
about the subject. Thanks




是的,函数定义用作原型。有些人认为

总是在第一次使用之前定义一个函数,有些人相信在文件开头提供一组原型。


此外,当您将程序拆分为多个源文件时,您应该使用

头文件来为一个文件中定义的函数提供原型

并在另一个中使用,并在文件中包含标题,用于定义

函数以及使用它来确保一致性

的函数。

-

马克戈登

支付成为极客&高级软件开发人员

虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。



Yes, the function definition serves as a prototype. Some people believe
in always defining a function before it is first used, some people
believe in providing a set of prototypes at the start of the file.

Also, when you split a program into multiple source files you should use
header files to provide the prototypes for functions defined in one file
and used in another and include the header in the file that defines the
function as well as the one that uses it to ensure consistency
throughout.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.


这篇关于有几个问题了。功能原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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