有没有一种方法可以用一种类型声明多个函数参数? [英] Is there a way to declare multiple function arguments with one type?

查看:82
本文介绍了有没有一种方法可以用一种类型声明多个函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程很陌生,无法真正理解为什么我不能只声明参数类型与普通变量一样,必须一次又一次地声明类型.

I'm pretty new to programming and can't really understand why I can't just declare argument types the same way I do with normal variables and have to declare the type again and again.

我的意思是,为什么我必须

I mean, why must I:

Func(int a, int b, float c)

代替

Func(int a, b, float c)

?

当然,只要它们是同一类型.

As long as they're the same type, of course.

  • 我真的可以这样做,只是不知道怎么做吗?

如果有可能 ,请告诉我怎么做.

If it is possible, please tell me how.

谢谢.

@ 0x499602D2:如果参数声明与对象声明更加相似,则void f(int a,float c,d)可能等效于void f(int a,float c,浮动d).该语言可以使此工作正确且一致地进行.只是没有.–基思·汤普森(Keith Thompson)

@0x499602D2: If parameter declarations were more closely analagous to object declarations, then void f(int a, float c, d) would presumably be equivalent to void f(int a, float c, float d). The language could have made this work correctly and consistently. It just didn't. – Keith Thompson

这最好地回答了我的问题.但这是一条评论...

This answered my question best. but it's a comment...

推荐答案

原因如下:

所有内容都有一些规则或适用于合同.从理论上讲,您可以编写一个C编译器来代替:

Everything has some rules or works on contracts. In theory you could write a C compiler that will instead of:

func(int a, int b)

接受:

func(int a, b)

那会很好.

但是

C的创建者决定,每个单独的形式参数都必须附加其类型,因此我们今天有了它.这只是您必须遵循的约定.

Creators of C decided that every single formal argument has to have its type attached to it hence we have it today. It's just a convention which you must follow.

并且您必须遵循它,因为C/C ++解析器期望您以这种方式进行操作,否则它将无法理解您.

And you must follow it as C/C++ parser is expecting you to do it this way otherwise it will not understand you.

类似地您的问题:

是否可以使用一种类型声明多个函数参数?

Is there a way to declare multiple function arguments with one type?

理论上可以这样写:

有多种方法可以用一种类型声明函数参数?

there multiple a way Is to declare function arguments with one type?

如果您同意某人以这种方式提出问题,则必须遵守此合同-期限.

If you agree with someone to construct questions this way you must follow this contract - period.

这篇关于有没有一种方法可以用一种类型声明多个函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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