473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

static function?

Hello,

I found some function like

void static foo(...){
....
}

what does the static keyword stand for? what is a static function?

thanx
Nov 14 '05 #1
12 2465
dual0 <du***@bluewin.ch> scribbled the following:
Hello, I found some function like void static foo(...){
...
} what does the static keyword stand for? what is a static function?


Enforces static linkage. This means that this function cannot be called
directly from other translation units, because they won't know its name
(it's not linked against them). You can still pass a function pointer
pointing to it and call through that.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #2
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.

Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?
Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
nethlek
Nov 14 '05 #4
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
Hello,

I found some function like

void static foo(...){
...
}

what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?
Note that "static" used at block scope (within a function to define data)
has a different meaning: it causes ordinarily "automatic" data to have
static storage duration, meaning the values persist (retain their value)
across multiple calls to the function in which they're defined. This
allows, for example, a function to keep track of how many times it was
called, or to define an array of values that only has to be initialized
once (and in C, such initialization happens at compile time.)
-leor

thanx


--
nethlek
Nov 14 '05 #5
In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote:
But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?


I do that as a rule of thumb. It doesn't hurt.

(kinda pseudo-code)

/* main.c */

/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
int main()
{
a();
b();
}

If the program grows, some of the static could become external functions. In
that case, they will loose the 'static', and some prefix will probably added
to their name. The separated compilation process will be implemented
(separated TU, header etc.)

/* main.c */

#include x.h"

/* private functions */
static a()
{
}

/* entry point */
int main()
{
a();
X_b();
}

/* x.h */
X_b();
/* x.c */
/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
X_b()
{
a();
b();
}

Here are the skeletons I use for code layouts :

Interface (.h)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

Implementation (.c)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #6
In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote:
But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon? Do other people here(comp.lang.c) do
this?


I do that as a rule of thumb. It doesn't hurt.

(kinda pseudo-code)

/* main.c */

/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
int main()
{
a();
b();
}

If the program grows, some of the static could become external functions. In
that case, they will loose the 'static', and some prefix will probably added
to their name. The separated compilation process will be implemented
(separated TU, header etc.)

/* main.c */

#include x.h"

/* private functions */
static a()
{
}

/* entry point */
int main()
{
a();
X_b();
}

/* x.h */
X_b();
/* x.c */
/* private functions */
static a()
{
}

static b()
{
}

/* entry point */
X_b()
{
a();
b();
}

Here are the skeletons I use for code layouts :

Interface (.h)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

Implementation (.c)

/* macros ================================================== ============ */
/* constants ================================================== ========= */
/* types ================================================== ============= */
/* structures ================================================== ======== */
/* private variables ================================================== = */
/* private functions ================================================== = */
/* internal public functions =========================================== */
/* entry points ================================================== ====== */
/* public variables ================================================== == */

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
ne*****@tokyo.com (Mantorok Redgormor) writes:
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
>Hello,
>
>I found some function like
>
>void static foo(...){
>...
>}
>
>what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon?


I don't think so.
Do other people here(comp.lang.c) do this?


Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #8
ne*****@tokyo.com (Mantorok Redgormor) writes:
Leor Zolman <le**@bdsoft.com> wrote in message news:<v1********************************@4ax.com>. ..
On 3 Apr 2004 08:16:27 -0800, du***@bluewin.ch (dual0) wrote:
>Hello,
>
>I found some function like
>
>void static foo(...){
>...
>}
>
>what does the static keyword stand for? what is a static function?
When used at file scope (at the "top level" of the source file), the static
modifier gives a name (for a function or data) "internal linkage"; this
means, basically, that the name of the object you're defining will not be
visible to the linker when you link this module with other modules. Thus
you can have a /different/ version of your "foo" function in each module
("translation unit") of your program. This can be handy for preventing
accidental name collisions across TU's.

Within a single TU, such use of static is transparent. It really only
affects how declarations of data and functions in /other/ TU's get (or
don't get) resolved.


Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something
which is frowned upon?


I don't think so.
Do other people here(comp.lang.c) do this?


Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #9
Mantorok Redgormor wrote:
.... snip ...
Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something which
is frowned upon? Do other people here(comp.lang.c) do this?


I tend to mark all functions as static whenever possible, i.e.
whenever I do not explicitly want access to them from other
modules. The advantage is that I can immediately see that such
functions are not referenced externally, no matter how the overall
system is modified, and thus that I can limit usage checks to the
single module during maintenance.

This also implies that such static functions _do not_ appear as
prototypes in any header file. I can think of no reason the word
static should ever appear in a header.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
Nov 14 '05 #10
Mantorok Redgormor wrote:
.... snip ...
Perhaps. But I use static on functions when I write a program
that consists of one translation unit. Is this something which
is frowned upon? Do other people here(comp.lang.c) do this?


I tend to mark all functions as static whenever possible, i.e.
whenever I do not explicitly want access to them from other
modules. The advantage is that I can immediately see that such
functions are not referenced externally, no matter how the overall
system is modified, and thus that I can limit usage checks to the
single module during maintenance.

This also implies that such static functions _do not_ appear as
prototypes in any header file. I can think of no reason the word
static should ever appear in a header.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
Nov 14 '05 #11
In <cu*************@zero-based.org> Martin Dickopp <ex****************@zero-based.org> writes:
Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.


What's appropriate for a monolithic program is not necessarily still
appropriate when the program is split into multiple source code files.
To minimise the pain, just don't use static in the monolithic program.
Only *after* splitting you can decide what should be declared as static.

The right thing is to try to anticipate whether the program will remain
a monolithic program for the rest of its life or whether it should be
designed as a multiple source code file program from the very beginning
(even if its initial version doesn't really justify a multi source file
approach). My crystal ball is excellent for this kind of guessing ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <cu*************@zero-based.org> Martin Dickopp <ex****************@zero-based.org> writes:
Yes, I do. What's one TU today might be split and/or extended to
multiple TUs tomorrow, so I consider it good style to use `static'
where appropriate right from the beginning.


What's appropriate for a monolithic program is not necessarily still
appropriate when the program is split into multiple source code files.
To minimise the pain, just don't use static in the monolithic program.
Only *after* splitting you can decide what should be declared as static.

The right thing is to try to anticipate whether the program will remain
a monolithic program for the rest of its life or whether it should be
designed as a multiple source code file program from the very beginning
(even if its initial version doesn't really justify a multi source file
approach). My crystal ball is excellent for this kind of guessing ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean-Francois Brault | last post by:
I wrote a crappy class for radian angle management. The class consists of an array of radian values. I put all these things in a class in which all methods are static, so I can access it anywhere...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.