-1

Say I declare a variable in one function like so:

void i() { int c; }

how would I be able to do this in a different function?

void j() {
    cout << c << endl;
}
Stan
  • 27
  • 5

4 Answers4

2

Lets start with the basics:

in C++, default storage class of variables is auto.

What this means is that variables declared inside a block will only be available inside that block after you declare them.

a block is marked with the curly braces (loops, if statements, functions, etc)

void i() {

     //c is not declared here and is unavailable
     int c;
     //c is available 

        for(int i=0; i<2; i++)
        {
         //c is also available here, because it is declared in larger scope
        }
}
//c is not recognized here because it is declared only inside the scope of i()

 void j()
 {
  int c; //this is variable c. it has nothing to do with the c that is declared in i()

        for(int i=0; i<2; i++)
        {
         int d; //this variable gets created and destroyed every iteration
        }

  //d is not available here.
 }

By default, a variable that is declared inside the body of a function, will live only inside that function scope after the point of declaration.

In your question you have not described how you want the two functions to communicate, thus it is difficult to point to a suitable approach.

If you want to pass the same value between functions you should read into argument passing and function return values. There is another way which is global variables, not recommended unless absolutely needed, but definitely something you need to learn.

ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
1
void j(const int c)
{
  std::cout << c << std::endl;
}

Then just call it from your function using:

j(c);
soulsabr
  • 895
  • 4
  • 16
  • only one possible approach, why pass c as const by the way? – ForeverStudent Jan 28 '16 at 21:15
  • Good habit to get into IMHO for a plethora of reasons too numerous to get into in a side comment. http://stackoverflow.com/questions/117293/use-of-const-for-function-parameters has some good arguments for and against the use of const. – soulsabr Jan 28 '16 at 21:50
1

The best approach depends on what you actually do with c. Namely if c is not unique to an invocation of i() then you can use a global variable to get access it anywhere:

int c;
void i(){
  c = c++; //uses c, pun intended
}
void j(){
  cout << c << endl;
  c++; //can change c
}

If a unique c is used every time i()is called, then you can define it in i() and have to pass it to j() from within i(), otherwise j() does not know where c is:

void i(){
  int c;
  j(c);
}
void j(int c){
  cout << c << endl;
  c++; //cannot change c in i()
}

Note that above c in i() and c in j() are not the same variable. If you change c in j() it will not be changed in i(), where as with the global approach it would be.

If want the variable to be unique to i() but accessible to j() you would instead pass the variable either by a pointer(not shown) or refence(shown below):

void i(){
  int c=0;
  j(c);
}
void j(int &c){
  cout << c << endl;
  c++; //can change c in i()
}

Note: The terminology here is rather casual, I didn't even use the word scope which you may want to learn more about.

Linus
  • 894
  • 7
  • 13
-2

The variable c wouldn't exist in the context where j is called, so you'd have to pass it on as a global variable or a return value.

int i(){
    int c = 2;
    return c;
}

int i() will return c, which can be passed on to other constructors.

void j(const int c){
    std::cout << c << std::endl;
}

Or rather, call i inside of j.

void j(){
    int cr = i();
    std::cout << cr << std::endl
}
ZetaRift
  • 332
  • 1
  • 9
  • His/Her examples use simple functions; not constructors. The worst he/she would have to worry about is forward declaring the functions not creating a class/struct. – soulsabr Jan 28 '16 at 21:09