且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么用途无效?

更新时间:2023-11-02 20:46:34

L +> +++


E-W + N +!o> + w ---

!O-!M-t +!5! X R + tv-b ++ DI +!DG e-> +++ h!




Ian Tuomi写道
你好,我想知道,如果你写的话会有什么不同

void foo(int x)

/ * insert code这里* /
}


foo(int x)
{
/ *在这里插入代码* /
}

有什么区别?两种方式的功能都不会返回任何东西,对吧?写空虚有什么意义?它只是一个美化特征吗?




绝对不是。如果省略函数声明中的返回类型,则

返回类型默认为int,所以


foo(int x)


实际上是


int foo(int x)


void类型用作返回的类型通过不产生

值的函数。

---

用户

>

Hello, I was wondering, does it make any difference if you write

void foo(int x)
{
/* insert code here */
}

or

foo(int x)
{
/* insert code here */
}

What is the difference?? Both ways the function wont return anything,
right? What is the point in writing void? Is it merely a cosmetic feature?

--
Ian Tuomi
Jyv?skyl?, Finland

GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

L+>+++


E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!



Ian Tuomi wrote
Hello, I was wondering, does it make any difference if you write

void foo(int x)
{
/* insert code here */
}

or

foo(int x)
{
/* insert code here */
}

What is the difference?? Both ways the function wont return anything,
right? What is the point in writing void? Is it merely a cosmetic feature?



Absolutely not. If you omit a return type from a function declaration, the
return type defaults to `int'', so

foo(int x)

is actually

int foo(int x)

The void type is used as the type returned by functions that generate no
value.
---
user