函数和变量声明 [英] Function and variable declarations

查看:76
本文介绍了函数和变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这里是前面提到的(也是有争议的)GNU

科学图书馆(GSL)的简短功能,或者更确切地说,是我稍微改写了它的版本

。该函数采用整数并为随机数生成器设置某些

必要值。 x,n和shuffle在

这个函数是全局变量。


/ ***************** ********************************* *********** /

static void

ran1_set(unsigned long int s){

int i;


if(s == 0 )

s = 1;


for(i = 0; i< 8; ++ i){

long int h = s / q;

long int t = a *(s - h * q) - h * r;

if(t< 0)

t + = m;

s = t;

}


for(i = N_SHUFFLE-1; i> = 0; - i){

long int h = s / q;

long int t = a *(s - h * q) - h * r;

if(t< 0)

t + = m;

s = t;

shuffle [i] = s;

}


x = s;

n = s;


返回;

}

/ ****************************** ******************** *********** /


无论如何,有两个问题:


(1)为什么声明函数astatic void而不只是虚空? (我的

个人使用静态只是为了保留不同的模块或函数调用之间的值,但我知道它的使用可能更复杂

比这个。)


(2)任何特殊原因为什么long int''sh和t被宣告两次

在不同的循环内,而不仅仅是在开头宣布

的功能?


非常感谢,


- Joe

解决方案



" Joseph Wakeling" <乔************* @ gmail.com>在消息中写道

news:11 ********************* @ o13g2000cwo.googlegro ups.com ...

大家好,

这里是前面提到的(也是有争议的)GNU
科学图书馆(GSL)的一个简短功能,或者更确切地说,是一个稍微重写过的版本
我已经做了。该函数采用整数并为随机数生成器设置某些必要的值。 x,n和shuffle这个函数是全局变量。

/ ************************* ************************* *********** /
静态无效
ran1_set(未签名long int s){
int i;

if(s == 0)
s = 1;

for(i = 0; i< ; 8; ++ i){
long int h = s / q;
long int t = a *(s - h * q) - h * r;
if(t< ; 0)
t + = m;
s = t;
}
对于(i = N_SHUFFLE-1; i> = 0; - i) {
long int h = s / q;
long int t = a *(s - h * q) - h * r;
if(t <0)
t + = m;
s = t;
shuffle [i] = s;
}

x = s;
n = s;

返回;
}
/ ******************************* ******************* *********** /

无论如何,有两个问题:

/>(1)为什么声明函数astatic void而不只是虚空? (我个人使用静态只是为了保持对模块或函数的不同调用之间的值,但我知道它的使用可能比这更复杂。)

(2)为什么long int''sh和t在不同的循环中声明两次
而不是仅仅在函数的开头声明?



1.对一个函数使用static会限制函数的可见性 -

翻译单元(文件)的那个''定义于。


2.据我所知 - Nope。


Joseph Wakeling写道:

static void
ran1_set(unsigned long int s){
< snip> for(i = 0; i< 8; ++ i){
long int h = s / q;
long int t = a *(s - h * q) - h * r; < (i = N_SHUFFLE-1; i> = 0; - i){
long int h = s / q;
long int t = a *(s - h * q) - h * r;
if(t< ; 0)
t + = m;
s = t;
shuffle [i] = s;
}
< snip> (1)为什么声明函数astatic void而不只是虚空? (我个人使用静态只是为了保持对模块或函数的不同调用之间的值,但我知道它的使用可能比这更复杂。)

" static"在声明中有两个不同的含义;一个是给一个

标识符静态存储(用你描述的用途),另一个是给它

它内部链接(这个用途)。这里的静态只是意味着不是extern,

即,功能符号是单位内部的,并且无法访问

来自外部。这提高了模块性。


C99中实际上有第三个含义,用于传递数组

参数,但是如果你对此感兴趣的话你可以查查看。你还不太可能在野外遇到它。而且我们会忽略C ++,其中
赋予它另一个含义。

(2)为什么long int''sh和t被宣告两次的任何特殊原因
在不同的循环中,而不是仅仅在函数的开头声明?



在它们所使用的最里面的块中声明变量有利于

可读性也可能对寄存器使用产生积极影响。在这个

的情况下,它并没有给你带来太多的好处,但也不会对任何人造成伤害。


S.


Joseph Wakeling写道:

大家好,

这里是上述(和有争议的)GNU的简短功能
科学图书馆(GSL),或者更确切地说,是我已经制作的一个稍微重写的版本。该函数采用整数并为随机数生成器设置某些必要的值。 x,n和shuffle这个函数是全局变量。

/ ************************* ************************* *********** /
静态无效
ran1_set(未签名long int s){
int i;

if(s == 0)
s = 1;

for(i = 0; i< ; 8; ++ i){
long int h = s / q;
long int t = a *(s - h * q) - h * r;
if(t< ; 0)
t + = m;
s = t;
}
对于(i = N_SHUFFLE-1; i> = 0; - i) {
long int h = s / q;
long int t = a *(s - h * q) - h * r;
if(t <0)
t + = m;
s = t;
shuffle [i] = s;
}
x = s;
n = s;


名为x和n的全局(或至少是文件范围)变量? Yuk。

返回;


返回这里恕不另行恕我直言。

}
/ **************** ********************************** *********** /

无论如何,有两个问题:

(1)为什么声明函数astatic void而不只是虚空? (我个人使用静态只是为了保持对模块或函数的不同调用之间的值,但我知道它的使用可能比这更复杂。)


当在函数或文件范围的变量上使用时,它意味着b $ b大约不能将该名称用于其他模块。或者,对于
使用标准术语,它只有内部联系。

(2)为什么long int''sh和t被声明两次的任何特殊原因
在不同的循环中,而不仅仅是在函数的开头声明?




它被称为最小化范围。因为它们是在循环中声明的,所以你不必检查它们是否在外面使用它们。

否则,你必须阅读超出结束时循环查看

最后一个值是否在循环外使用。

-

Flash Gordon

生活在有趣的时间。

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


Hello all,

Here''s a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I''ve made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/************************************************** ***********/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
}

x = s;
n = s;

return;
}
/************************************************** ***********/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

(2) Any particular reason why the long int''s h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?

Many thanks,

-- Joe

解决方案


"Joseph Wakeling" <jo*************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Hello all,

Here''s a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I''ve made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/************************************************** ***********/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
}

x = s;
n = s;

return;
}
/************************************************** ***********/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

(2) Any particular reason why the long int''s h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?



1. The use of static for a function restricts the function''s visibility - to
that of the translation unit (file) it''s defined in.

2. As far as I know - Nope.


Joseph Wakeling wrote:

static void
ran1_set (unsigned long int s) { <snip> for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
} <snip> (1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)
"static" in declarations has two different meanings; one is to give an
identifier static storage (the use you describe), the other is to give
it internal linkage (this use). Here "static" just means "not extern",
that is, the function symbol is internal to the unit and inaccessible
from the outside. This improves modularity.

There''s actually a third meaning in C99, used in passing array
parameters, but if you''re interested in this you can look it up. You''re
not likely to encounter it in the wild yet. And we''ll ignore C++, which
assigns yet another meaning to it.
(2) Any particular reason why the long int''s h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?


Declaring variables in the innermost block they are used in is good for
readability and may have positive effects on register usage too. In this
case it doesn''t buy you much, but it''s not hurting anyone either.

S.


Joseph Wakeling wrote:

Hello all,

Here''s a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I''ve made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/************************************************** ***********/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
}

x = s;
n = s;
Global (or at least file scope) variable named x and n? Yuk.
return;
Rather pointless having a return here IMHO.
}
/************************************************** ***********/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)
When used on a function or a variable at file scope it means
approximately "don''t make the name available to other modules." Or, to
use standard terminology, it only has internal linkage.
(2) Any particular reason why the long int''s h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?



It''s called minimising scope. Because they are declared in the loop you
know, without having to check, that they are not used outside.
Otherwise, you would have to read beyond the end of the loop to see if
the last value gets used outside the loop.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于函数和变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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