473,385 Members | 1,863 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,385 software developers and data experts.

static in function definition

What is the difference between following function definitions:

static int f1()
{
/* code here */
}

and

int f1()
{
/* code here */
}

Aug 27 '07 #1
6 2575

"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...
What is the difference between following function definitions:

static int f1()
{
/* code here */
}

and

int f1()
{
/* code here */
}
The function declared static can only be called from another function within
the same file. This is very useful in complex projects, because it
simplifies the connections between modules.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 27 '07 #2
Malcolm McLean wrote, On 27/08/07 21:57:
>
"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...
>What is the difference between following function definitions:

static int f1()
{
/* code here */
}

and

int f1()
{
/* code here */
}

The function declared static can only be called from another function
within the same file.
Incorrect. If can only be called *directly* from within the same
translation unit. Function pointers can and do get passed around
allowing functions declared static to be called from other translation
units.
This is very useful in complex projects, because
it simplifies the connections between modules.
True.
--
Flash Gordon
Aug 27 '07 #3
"Malcolm McLean" <re*******@btinternet.comwrites:
"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...
>What is the difference between following function definitions:
static int f1()
{
/* code here */
}

and

int f1()
{
/* code here */
}

The function declared static can only be called from another function
within the same file. This is very useful in complex projects, because
it simplifies the connections between modules.
Actually, it means that the name is only visible within the same
translation unit (which, with #include directives, could consiste of
multiple files). If you take the address of f1, you can call it
indirectly from anywhere.

In most cases, you wouldn't want to do this; if you want to call it
from another translation unit, you don't make it static. But I can
think of cases where you'd want to be able to call a function
indirectly without making its name directly visible.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 27 '07 #4
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
[...]
In most cases, you wouldn't want to do this; if you want to call it
from another translation unit, you don't make it static. But I can
think of cases where you'd want to be able to call a function
indirectly without making its name directly visible.
[...]

Here is an example of using a table of pointers to "private" static
functions in a translation unit for use as a vtable in a minimalist
object-oriented C technique:

http://groups.google.com/group/comp....106926ba5db19f

So, IMHO, there are very legitimate reasons to pass function pointers to
static functions.

Aug 27 '07 #5
Ravi wrote:
>
What is the difference between following function definitions:

static int f1() {
/* code here */
}

and

int f1() {
/* code here */
}
The static function can only be called by code within the same
file, or code that has been passed a functional pointer to f1. The
name does not escape the particular source file involved. The
non-static name is visibile in all files at link time, and can
cause multi-defined errors, or peacefully link up as designed.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 28 '07 #6

Keith Thompson wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
"Ravi" <ra*********@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...
What is the difference between following function definitions:
static int f1()
{
/* code here */
}

and

int f1()
{
/* code here */
}
The function declared static can only be called from another function
within the same file. This is very useful in complex projects, because
it simplifies the connections between modules.
It plays a great role in terms of scope of the function between
files .
It hides the name of this function from the other files so they cannot
use/call it.
But, there are ways to indirectly overcome that 'static' effect.
That is, Pointers to this function (Function pointers) can nullify the
static and make it accessible
across all the files.
>
Actually, it means that the name is only visible within the same
translation unit (which, with #include directives, could consiste of
multiple files). If you take the address of f1, you can call it
indirectly from anywhere.
Yeah, using the address, we can call it from anywhere.

Karthik Balaguru

Aug 28 '07 #7

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

Similar topics

24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
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...
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
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...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
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...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.