function with static scope. 
Author Message
 function with static scope.

anyone know what is the advantage for doing:

static int myFunc()

compare to

int myFunc()



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.


Quote:
> anyone know what is the advantage for doing:

> static int myFunc()

> compare to

> int myFunc()

If you're talking C, rather than C++...

It hides the function from external use, thus saving the namespace from
pollution. i.e. You can have a number of myFunc() functions in
different code units.

If you're talking C++, then I'd be afraid to answer in this particular
newsgroup. (But the words, "class method", "one per instance" and
"static member variable access only", might slip out).

;)

J.

--
"These thoughts, and the strain I am under." - TY

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:

> anyone know what is the advantage for doing:

> static int myFunc()

This function can be called only by functions in the same translation
unit (roughly, the same file).

Quote:
> compare to

> int myFunc()

This function can be called by all other functions in the same program.

Richard



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:
EGoh writes:
>anyone know what is the advantage for doing:

>static int myFunc()

>compare to

>int myFunc()

The default scope of a function is to be visisble in all the files that make up
the program.  Adding static restricts its visibility to only the file that
contains the definition.  So, on  a large project, the writer of myFunc()
doesn't pollute the gloabal namespace.  No one else will accidentally call it.
and so on.  Many, if not most, of the advantages of privacy.

Where I use 'file', language lawyers use 'translation unit'.  WRT this aspect,
functions and variables are treated differently.  



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.
The keyword static in this case refers to the function myFunc()
making it a static function just like the main function.
i.e. when the function is called again its previous state is maintained.
In other words the function is not refreshed each time it is called.

-CSK
http://snap.to/csk


Quote:
> anyone know what is the advantage for doing:

> static int myFunc()

> compare to

> int myFunc()



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.
But I can achieve the same effect by simply put the function prototype
"int myFunct()" in *.c file where it will has file scope as oppose to using
static keyword.


Quote:

> > anyone know what is the advantage for doing:

> > static int myFunc()

> This function can be called only by functions in the same translation
> unit (roughly, the same file).

> > compare to

> > int myFunc()

> This function can be called by all other functions in the same program.

> Richard



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.


Quote:



> > anyone know what is the advantage for doing:

> > static int myFunc()

> > compare to

> > int myFunc()

> The keyword static in this case refers to the function myFunc()
> making it a static function just like the main function.
> i.e. when the function is called again its previous state is
maintained.
> In other words the function is not refreshed each time it is called.

Dang.  I really hate it when C99 changes everything I used to
know about C.  They must have added this in after the last draft
I downloaded.

--
MJSR

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:

>But I can achieve the same effect by simply put the function
>prototype "int myFunct()" in *.c file where it will has file
>scope as oppose to using static keyword.

You won't limit the scope that way.  The function will still be
visible outside of the file.

Richard.

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.
[Learn to quote, damnit. Added text goes _after_ quoted material.]

Quote:


> > > anyone know what is the advantage for doing:

> > > static int myFunc()

> > This function can be called only by functions in the same translation
> > unit (roughly, the same file).

> > > compare to

> > > int myFunc()

> > This function can be called by all other functions in the same program.

> But I can achieve the same effect by simply put the function prototype
> "int myFunct()" in *.c file where it will has file scope as oppose to using
> static keyword.

Since you don't say which of the two above you're replying to (see why
putting relpies _after_ the material you're replying to is better?) I
can only assume you mean that

static int myfunc()

and

int myfunc()

have the same effect. This is simply not true. The former can not be
called from outside its own file. The latter can. While it is true that
it is not the wisest thing to do to call a function that has not got a
prototype in scope, it can be done, at least in this case, where
myfunc() returns an int. In other cases, it can be _done_; but the
resulting return value is undefined, AFAIK.

Richard



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:
>The keyword static in this case refers to the function myFunc()
>making it a static function just like the main function.
>i.e. when the function is called again its previous state is maintained.
>In other words the function is not refreshed each time it is called.

Can you explain what a function state is ? I am interested. What do you mean
by refreshed ? Cooled ?

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:

>The keyword static in this case refers to the function myFunc()
>making it a static function just like the main function.
>i.e. when the function is called again its previous state is maintained.
>In other words the function is not refreshed each time it is called.

What are you on about?  This is utter nonsense.  main() is not, and cannot
be, a static function, and the static keyword has nothing to do with "state"
of a function.  The only meaning "static" has with respect to functions is
to give the function's name "internal linkage", meaning that the function's
name is not known outside of its own translation unit (source file).
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies


Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.

Quote:


> [Learn to quote, damnit. Added text goes _after_ quoted material.]



> > > > anyone know what is the advantage for doing:

> > > > static int myFunc()

> > > This function can be called only by functions in the same translation
> > > unit (roughly, the same file).

> > > > compare to

> > > > int myFunc()

> > > This function can be called by all other functions in the same program.

> > But I can achieve the same effect by simply put the function prototype
> > "int myFunct()" in *.c file where it will has file scope as oppose to using
> > static keyword.

Declarations never have "global" scope. That is, any declaration will
(at most) only be visible in the translation unit (henceforth called
"file") it appears in. The 'static' keyword affects something else,
called "linkage".

A function declaration such as: 'int myfunc()' has "external linkage"
(provided it doesn't appear within a block). That means, if the same
declaration appears in a different file, explicitly or implicitly (e.g.
by calling it), it will refer to the same function.

A declaration such as 'static int myfunc()' has "internal linkage"
(given the above provision). That is, it isn't the same as any
declaration found in a different file, giving, in effect, a function
that is "private" to this file, and any declaration of 'myfunc()' found
outside this file will refer to a different 'myfunc()'.

Quote:
> static int myfunc()

> and

> int myfunc()

> have the same effect. This is simply not true. The former can not be
> called from outside its own file. The latter can. While it is true that

Incidentally, it /can/ be called outside its own file, using function
pointers.

--
Joe



Fri, 23 Aug 2002 03:00:00 GMT  
 function with static scope.


Quote:
> The keyword static in this case refers to the function myFunc()
> making it a static function just like the main function.
> i.e. when the function is called again its previous state is maintained.
> In other words the function is not refreshed each time it is called.

I think you must be confusing C with some other language which uses the
keyword "static" (heaven knows, there's enough of them around).

In C, declaring a function `static` just makes it invisible outside
its compilation unit. (Not inaccessible, because of function pointers;
just invisible, in that you can't refer to it by name.)

--
Chris "non-static" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sat, 24 Aug 2002 03:00:00 GMT  
 function with static scope.
Quote:





> > > anyone know what is the advantage for doing:

> > > static int myFunc()

> > > compare to

> > > int myFunc()

*> > The keyword static in this case refers to the function myFunc()
*> > making it a static function just like the main function.
*> > i.e. when the function is called again its previous state is
*> maintained.
*> > In other words the function is not refreshed each time it is
called.
*>
Quote:
> Dang.  I really hate it when C99 changes everything I used to
> know about C.  They must have added this in after the last draft
> I downloaded.

> --
> MJSR

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Don't worry. The starred text above is just plain wrong.
All "static" means when applied to a function is that it has
"translation unit" (roughly file) scope.
HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
A: Look for a lawyer who speaks Aramaic...about trademark infringement.


Sat, 24 Aug 2002 03:00:00 GMT  
 function with static scope.
Oops! I've confused static functions with static variables
CSK


Quote:
> The keyword static in this case refers to the function myFunc()
> making it a static function just like the main function.
> i.e. when the function is called again its previous state is maintained.
> In other words the function is not refreshed each time it is called.

> -CSK
> http://snap.to/csk



> > anyone know what is the advantage for doing:

> > static int myFunc()

> > compare to

> > int myFunc()



Sun, 25 Aug 2002 03:00:00 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Static functions and file scope

2. Static scope in static library

3. Block-Scope vs Function-Scope, Speed Difference?

4. One Problem in Calling Static function from Non static function

5. Accessing non-static functions from a static function

6. Scope of static class fields question.

7. static scope in asp.net, IIS 5

8. scope - static/extern

9. Static keyword: scope for confusion!

10. Scope of 'static' variables

11. local static const array scope?

12. MSVC 5.0 static file-scope init order bug

 

 
Powered by phpBB® Forum Software