0

I would like to know how I can make a function's variable public to other functions.

Example:

void InHere
{
    int one = 1; // I want to be public
}
int main()
{
    InHere(); // This will set int one = 1
    one = 2; // If the variable is public, I should be able to do this
    return 0;
}

Does anyone know how to do this? The only things I find when searching is for classes, as you can see nothing is in a class and I don't want them to be in one.

Any help is really appreciated!

JohnJameson
  • 81
  • 1
  • 12
  • 3
    C++ doesn't work this way. If you want the variables to be accessible beyond a single function, they need to live in a class or a namespace. There's no way for something other than `InHere` to know about `one`. – Nathan Pierson Jul 18 '21 at 03:27
  • 1
    Public variables to other functions are global variables. – 273K Jul 18 '21 at 03:32

2 Answers2

1

A variable defined locally to a function is generally inaccessible outside that function unless the function explicitly supplies a reference/pointer to that variable.

One option is for the function to explicitly return a reference or pointer to that variable to the caller. That gives undefined behaviour if the variable is not static, as it does not exist after the function returns.

int &InHere()
{
     static int one = 1;
     return one;
}

void some_other_func()
{
     InHere() = 2;
}

This causes undefined behaviour if the variable one is not static since, as far as the program as a whole is concerned, the variable only comes into existence whes InHere() is called and ceases to exist as it returns (so the caller receives a dangling reference - a reference to something that no longer exists).

Another option is for the function to pass a pointer or reference to the variable as an argument to another function.

 void do_something(int &variable)
 {
      variable = 2;
 }     

 int InHere()
 {
      int one = 1;
      do_something(one);
      std::cout << one << '\n';    // will print 2
 }

The downside is that this only provides access to functions CALLED BY InHere(). Although the variable does not need to be static in this case, the variable still ceases to exist as InHere() returns (so if you want to combine option 1 and option 2 in some way, the variable needs to be static)

A third option is to define the variable at file scope, so it has static storage duration (i.e. its lifetime is not related to the function);

 int one;
 void InHere()
 {
      one = 1;
 }

 void another_function()
 {
      one = 2;
 }

 int main()
 {
     InHere();
        // one has value 1

     some_other_function();
        // one has value 2
 }

A global variable can be accessed in any function that has visibility of a declaration of the variable. For example, we could do

 extern int one;             // declaration but not definition of one

 int one;                    // definition of one.  This can only appear ONCE into the entire program

 void InHere()
 {
      one = 1;
 }

And, in other source file

 extern int one;         // this provides visibility to one but relies on it
                         //   being defined in another source file 

 void another_function()
 {
      one = 2;
 }

 int main()
 {
     InHere();
        // one has value 1

     some_other_function();
        // one has value 2
 }

Be careful with that though - there are numerous down-sides of global/static variables, to the extent they are usually considered VERY BAD programming technique. Have a look at this link (and pages linked to from there) for a description of some of the problems.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

Just set the variable as a global variable. Then you can access it from other functions.

int one;
void InHere()
{
    one = 1; // I want to be public
}
int main()
{
    InHere(); // This will set int one = 1
    one = 2; // If the variable is public, I should be able to do this
    return 0;
}

if you want it inside a class, then try the code below

#include <iostream>
using namespace std;

class My_class
{
    // private members
public: // public section
    // public members, methods or attributes
    int one;
    void InHere();
};

void My_class::InHere()
{
    one = 1; // it is public now
}
int main()
{
    My_class obj;
    obj.InHere(); // This will set one = 1
    cout<<obj.one;
    obj.one = 2; // If the variable is public, I should be able to do this
    cout<<obj.one;
    return 0;
}
Pranta Palit
  • 663
  • 2
  • 5
  • 15