What do u mean by static int f(x)

I want to know why do we declare a function in the following format static int f(int x);

static int f(int x)
{
...

....
}

why do we give the return type as static?






MasterdumboAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jasonclarkeCommented:
Two possibilities, i) a standalone, normal function:

static in this instance, means, effectively that the function is private to the translation unit (.cpp file) in which it is defined.  That is only code in the same file unit may call it.

It's name will also not conflict with a similar function in any other translation unit, so you could have another function with the same name and signature in another file without multiply defined symbols.

Possibility ii) a class member function:

a completely different meaning for static (helpful!), in this case it means that there is one instance of the function per class (not per object), i.e. it is a class method.  The method may be called without an instance of the class (Class::method()).

0
RanganathanVenkatakrishnanCommented:
static int F(int x) means as foolows

1. A function is declared static so that it can be accessed without creating the instance of the Class.
2. To access only static data members and functions.

3. The return type is not static.

eg
 class A
{
static int x;
int z;
public:
A();
static F(int y)
{
x = y;
z = y // not allowed
}
};

void main()
{
A a; // not required

A::F(20);

}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
Ranga_etc, how do you know Masterdumbo is asking about static class members?
0
jasonclarkeCommented:
RanganathanVenkatakrishnan,

please read the guidelines for comments and answers.  Your contribution does not constitute a proper answer.

> To access only static data members and functions.

oh, really, how do you account for this then:

class X
{
    int x;
public:
    static f(X* obj) { obj->x = 2; }
};

> 3. The return type is not static.

what exactly do you mean by this?
0
AxterCommented:
Hi RanganathanVenkatakrishnan , welcome to Experts-Exchange.
All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

Proper etiquette for posting Answers:
1.  An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

2.  An answer should not be posted as an answer, if it has a question.
Example: (Is this for windows? If so, here?s the answer)

3.  An answer should not be posted as an answer, if it has the phrase ?I think?.
Example:  (I think this should work)

4.  Full complete schoolwork answers must not be posted. (EE Policy)

5.  If you inadvertently post an answer after anther expert just post the same answer as a comment, you should promptly notify the questioner of your accidental post, and ask the questioner to fairly award the question to the expert whom fully/best answered the question first.  You might consider also apologizing to the other expert.

If you feel you have an answer, you can post it as a comment, and the questioner can convert your comment to an answer if he/she thinks it?s correct or deserving of the awarded points.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.
If you read the following link, you?ll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp


Hi Masterdumbo:
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.