1

I want to get a value in main() from the functionTwo().
The functionOne is designed to calculate the sum of the functionTwo().

Why I cannot use this sentence to pass the j value to int b?

int b  = functionOne();
 //function 0ne
int functionOne(int x) {
    static int j = 0;
    j = j + x;
    return j;
}

//function Two
void functionTwo() {
    int a = 10;
    for (int i = 0; i < a; i++) {
        functionOne(2);
    }
}

// print the j value of functionOne in main()
int main()
{
    functionTwo();

    //Why I cannot use this sentence to pass the j value to "int b"?
    int b = functionOne();

    cout << "the j value of functionOne in main() is: " << functionOne << endl;
    return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
ashur
  • 25
  • 1
  • 8
  • What happens when you try? Are you perhaps getting an error? If so, add it to the question. – TheUndeadFish Dec 22 '20 at 08:12
  • Because that's not how C++ works? Perhaps what you're looking for are *classes* and *member variables* and *getters*? Please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take a couple of classes to learn C++ properly. – Some programmer dude Dec 22 '20 at 08:14
  • `functionTwo` does not calculate a sum - it just calls `functionOne`, where the addition happens, repeatedly - and `functionOne` takes an integer parameter. The function does not have a value "on its own", which it looks like you're assuming. – molbdnilo Dec 22 '20 at 08:15
  • 2
    Why not to do things the normal way? When you want a function to return a value use `return` from that function, don't save the return value in a static variable for another function to return. You are just making things unnecessarily complicated for yourself. – john Dec 22 '20 at 08:19
  • @john That seems to be a solution based on a more fundamental change to cleaner design, rather than the quick-patch I proposed. I'd be happy to upvote a corresponding answer. Or maybe consider allowing (others or me) to turn your comment into an answer for you. – Yunnosch Dec 22 '20 at 08:32
  • @Yunnosch I try many ways to give you an upvote, but the system rejected my upvote. I don't know why. – ashur Dec 22 '20 at 08:37

1 Answers1

2

That int functionOne(int x) wants a parameter. That is why you cannot get it like you tried.

There is not clean way to do this, no intended way to get the value from a static variable inside a function. You do return it, but calling the function obviously has a side effect on the static variable, which is of course the purpose of this and practically any other static variable in any function.

In this special case you could get the value but not influence the static variables value, by

int b = functionOne(0);

For a more generic solution (for cases where any parameter value has undesired side effects) you should look into the concept of classes in C++. It allows to create objects which have a similar internal value, but also allow to read them out explicitly, without side effects, via "getter" methods. (The details on this were already mentioned in a comment on your question by john, hope they do not mind.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    @Someprogrammerdude My answer (an edit) overlaps with your comment, but it is not based on it (read it later...). But I asssume that you would not mind, even if it were. I mention this, because I worry that I got a downvote for that... – Yunnosch Dec 22 '20 at 08:30
  • I try to vote an upvote, but the system rejected my vote and I don't know why there is a downvote. Your answer is exactly what I want. – ashur Dec 22 '20 at 08:34
  • When I click one time, it's showing "zero upvotes", When I click two times, it's showing " -1 upvotes(downvote)." And When I click three times, it's showing "zero upvotes" again. But I wanna give you "+ 1 upvotes". – ashur Dec 22 '20 at 08:45
  • Now you got the score result you wanted. But generally you can only increase the score by one, not set it to a value. Somebody else downvoted (which they are entitled to do, even anonymously) and by now another user upvoted together with you, for a total of 1. Do not worry. – Yunnosch Dec 22 '20 at 08:47