Hello everyone! I am new to c++. I have to calculate a value through an equation and store that value each time whenever the function is being called. I have created an array of size 10 and calculated myValue then tried to store that value. I am not sure that this is a correct way to do so. I did not run a loop because if I'll run a loop here it will calculate 10 values in single call while I have to calculate and store 1 value in each call.
Duplicate post. Please don't do this - it's a time waster for those who reply. All it is going to do is lower our willingness to reply. What was wrong with the replies in the other topic?
@programmer007
Not a good idea to have global variables, the scope should be as tight as possible.
Send the variable to whatever function needs it. If sent by reference the it will be available it the outer scope. If the variable can be local to the function then do that.
Send the variable to whatever function needs it. If sent by reference the it will be available it the outer scope. If the variable can be local to the function then do that.
// Function prototype.
void function(int& c);
int main() /// Note that main should be defined to return an int.
{
int counter = 2;
function(counter);
}
void function(int& c) // Pass by reference.
{
c++;
}
C++ doesn't allow default return types you must explicitly state the function return types. You should also prefer passing by reference over passing by pointer whenever possible.
could you please explain with code? The way I think of it, I can't catch the value of counter in the code without using a complicated method (which requires returning value of counter with a if-statement and increasing the argument of function)
I suggest you write a program then ask questions on the code you provide.
I think of it, I can't catch the value of counter in the code without using a complicated method (which requires returning value of counter with a if-statement and increasing the argument of function)
That doesn't make a lot of sense. Perhaps you should post some code to illustrate your problem.
By the way when you do post the question I suggest you open your own topic instead of hijacking the topic of someone else.