473,738 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a real need to use keyword static with functions?

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 the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".

I don't know if this is a trivial matter since the reserved word
exists. :)
Or it exists for some other purpose I can't figure out.

What did I have missed? :)

Thanks in advance for answers

Mar 5 '07 #1
32 3041
lc**********@gm ail.com wrote:
Hi everyone

Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Yes, otherwise the functions have global scope and pollute the global
namespace. You may not want to export those symbols and you, or the
user of you code, will run into link problems if the same name is used
in more than one source file.

--
Ian Collins.
Mar 5 '07 #2
On Mar 5, 9:10 pm, lcdgoncal...@gm ail.com wrote:
Many textbooks avoid to discuss this matter and/or discuss only the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".
That's sad.

Do you have a database of all functions in your programs to make sure
that you don't use the same function name twice?

Mar 5 '07 #3
lc**********@gm ail.com writes:
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Yes. If two functions are defined without static, but with the
same name, in different translation units, then the result is
undefined behavior. Using static avoids this undefined behavior.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Mar 5 '07 #4
In article <87************ @blp.benpfaff.o rg>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>lc**********@g mail.com writes:
>Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?

Yes. If two functions are defined without static, but with the
same name, in different translation units, then the result is
undefined behavior. Using static avoids this undefined behavior.
Again, you guys are "talking around the problem". What you should be
reacting to is the implication (i.e., the OP's delusion) that neglecting
to declare a prototype in a header file somehow "hides" global symbols
found in translation units. I.e., makes it as if they had been declared
(gasp!) static.

Well, to the OP, 'taint so.

Mar 5 '07 #5
On Mar 5, 1:10 pm, lcdgoncal...@gm ail.com wrote:
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 the
usage of static functions. I've been programming for years and I never
felt a real need for the "static approach for functions".

I don't know if this is a trivial matter since the reserved word
exists. :)
Or it exists for some other purpose I can't figure out.

What did I have missed? :)

Thanks in advance for answers
Functions should always be made static if they are not intended as end-
user available routines.
Imagine some function called :
void HelperInitializ er(struct goombah * foo);
and it is only supposed to be called by *you* the programmer. It is
to be called only once and if it were to be called again, it would
reset the state of some of your objects in a way not intended. It is
essential to define this function as static or it *will* be available
to the end-user of the library whether you like it or not.

There are lots of other sensible reasons for static (some of which are
mentioned else-thread).

The author of that book wouldn't have been Herbert Schildt, would it?

Mar 5 '07 #6
In article <11************ *********@c51g2 000cwc.googlegr oups.com>,
<lc**********@g mail.comwrote:
>Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Bear in mind that there's nothing magic about .h files. They're just
some C code - usually, but not necessarily declarations - that can be
handily included in other files. So the fact that a function isn't
declared in a .h file doesn't stop some other part of the program from
using it, or from inadvertently using the same name for something else
(which may be detected when you link the program, or may just cause
some peculiar failure later on).

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 5 '07 #7
lc**********@gm ail.com wrote:
Hi everyone

Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Not necessary, but good practice.

If you use static then you can have two C files which both have
their own definition of my_function and the compiler will accept
it and do the right thing.

If you don't use static you will get a link time error about two
definitions of my_function.

HTH,
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
Failure is not an option. It comes bundled with your Microsoft
product.
Mar 5 '07 #8
Hi everyone,
Is there any real need for keyword "static inline" with
functions.

--pradeep.
On Mar 6, 3:59 am, Erik de Castro Lopo <e...@mega-nerd.comwrote:
lcdgoncal...@gm ail.com wrote:
Hi everyone
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?

Not necessary, but good practice.

If you use static then you can have two C files which both have
their own definition of my_function and the compiler will accept
it and do the right thing.

If you don't use static you will get a link time error about two
definitions of my_function.

HTH,
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo
+-----------------------------------------------------------+
Failure is not an option. It comes bundled with your Microsoft
product.

Mar 6 '07 #9
pradeep wrote:

Please don't top-post. Your reply should be below or interspersed with
the material you quote. Irrelevant text can be deleted, particularly
sig blocks that follow a '-- ' marker.
[fixed]
On Mar 6, 3:59 am, Erik de Castro Lopo <e...@mega-nerd.comwrote:
lcdgoncal...@gm ail.com wrote:
Hi everyone
Is there a real need to use keyword static with functions, if we
simply don't declare their prototypes in .h file?
Not necessary, but good practice.

If you use static then you can have two C files which both have
their own definition of my_function and the compiler will accept
it and do the right thing.

If you don't use static you will get a link time error about two
definitions of my_function.
Hi everyone,
Is there any real need for keyword "static inline" with
functions.
Well, static's effect has been explained in this thread. The inline
qualifier is a hint to the compiler to speed up access to the
function, if possible. Generally, it's used for very small functions
that're called in tight loops. The compiler may or may not actually
embed the function's body into each place where it's invoked. There
may be other methods of speeding up access to it.

inline is a C99 feature, unlike static.

Mar 6 '07 #10

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

Similar topics

3
1606
by: Der Andere | last post by:
Do non-member static functions exist? If yes, what is the sense of making a non-member function a static function? Can these functions only access static variables? Cheers, Matthias Treder
7
2155
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
4
1673
by: Ufit | last post by:
What is the difference between those two. Is there gonna be some difference in app execution for static functions? What difference does it make? UF
8
2838
by: prasi | last post by:
hi, all I wanted to know the significance of static functions. can anybody explain me. thanks in advance Prasanna K P
9
2178
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and variables?
5
7132
by: WebMatrix | last post by:
Hello, It might seem like a stupid question to some. But I need to put this issue to rest once and for all, since it keeps coming up in code reviews every now and then. There’s a static function in business logic class invoked by a Web multi-user application. Each request calls this static function passing in one unique ID, and XML serialized object (as out param), function deserializes XML to object, creates new instances of DB Access...
7
1895
by: sam_cit | last post by:
Hi Everyone, I had a look at a big project for real-world purpose and i found that all the functions in c have been prototyped as static and i understand that this would mean that these functions can be called only from code within that file, please let me know if there are any other reason to do this.
3
4046
by: llothar | last post by:
I'm using SmallEiffel - which is more or less a huge preprocessor - for C. It has the option to compile the whole Eiffel program into one huge C file but doesn't add a static declaration to the functions. I wonder if there are some clever optimizing compilers that do more magic (for example inlining) of static functions. It's not much time to patch the eiffel compiler, but if it is useless there are other things to waste time.
1
1582
by: wizwx | last post by:
I just noticed an interesting implementation of Singleton from Bruce Eckel's "Thinking in C++" vol. 2, pp 620: class Singleton { static Singleton s; public: static Singleton& instance() { return s; } // .... };
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.