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?

Who is Participating?
RanganathanVenkatakrishnanConnect With a Mentor Commented:
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.

 class A
static int x;
int z;
static F(int y)
x = y;
z = y // not allowed

void main()
A a; // not required



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()).

Ranga_etc, how do you know Masterdumbo is asking about static class members?

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;
    static f(X* obj) { obj->x = 2; }

> 3. The return type is not static.

what exactly do you mean by this?
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.


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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.