help me about declaring vairable faster and not dublicate

i have many functions in main()...but i just want 2 special functions which use the same vairables work well without declare vairables again and again
and dont tell me use global vairables which are used for whole function while i just need some of them share the same vairables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;

int nothing();
int main()
{
  int b;
  nothing();
  b=a+2;
  cout<<"i need vairable a can be used in this function without declaration cause i did it in nothing()'s function already" <<b;
  
}

int nothing()
{
  int a=1;
  return a;
}
Last edited on
Helllo phongvants123,

You have three options here:

1. Use the return value of the function:
1
2
3
4
5
int main()
{
  int b;
  int a = nothing();
  b=a+2;


2. Or pass "a" by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int nothing(int& a);

int main()
{
  int a{};  // <--- Always initialize your variables.
  int b;

  nothing(a);

  b=a+2;

  cout<<"i need vairable a can be used in this function without declaration cause i did it in nothing()'s function already" <<b;
  
  return 0;
}

int nothing(int& a)
{
   a=1;
 }


3. Use the function.
1
2
3
4
5
6
7
8
9
10
int main()
{
  int b;

  b = nothing() + 2;

  cout<<"i need vairable a can be used in this function without declaration cause i did it in nothing()'s function already" <<b;
  
  return 0;
}


One of these should work for you.

Hope that helps,

Andy

Edit:
Last edited on
there are 2 other ways.

1) if you wrapped the functions in a class, they could share the class member variables.

2) there are globals, and then there are globals :)

consider this questionable class.

template <typename type> class glob
{
public:
static type t;
};
template <typename type> type glob<type>::t;

void foo()
{
glob<int> x;
x.t = 11;
}

void bar()
{
glob<int> y;
cout << y.t << endl;
}

int main()
{
foo(); bar(); //prints 11, because x.t and y.t are the SAME variable. this is true for each template type, that is glob<double> would not be the same as glob<int> ... everything of the same type is the same, and different types are unique.
}


Last edited on
joihnn is to complicated and i can not understand
ANDY is easy to understand but non of them are usefull...for 3 reasons
method 1: declaring vairable a again which i really get rid of
method 2: int a{} is nothing diferent from declare int a 1 more time
method 3: my vairable a is almost disapear which is very important vairable for me to use in other function....
after all i expect your ethusialm
Last edited on
you may not seen classes yet. Classes are a programmer-created data type with functions. You will see them soon; they are the core of c++ programming really.

class do_nothing
{
int a;
int b;

int nothing() {a = 1; return a;} //can use shared a and b
int n2() { b = a+2; return b} //can use shared a and b
//inside the class, a and b are pretty much globals. Outside the class, they cannot be accessed.
};

int main()
{
do_nothing d;
int m_a = d.nothing(); //m_a is a copy of a from the class
int m_b = d.n2(); //m_b is a copy of b from the class
}

-------------
the other example is a backwards way to make a "global" via OOP. The syntax may be unfamiliar but that is all it is doing. Just like normal globals, this would introduce the same sorts of problems that globals have, and the same benefits.



Last edited on
I’d go with Handy Andy’s simple and effective solutions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Copied from Handy Andy's previous post
#include <iostream>

int nothing();

int main()
{
    int a = nothing();
    int b = a + 2;

    std::cout << "i need... " << b << '\n';
}

int nothing()
{
    int a = 1;
    return a;
}

method 1: declaring vairable a again which i really get rid of

This is *not* ‘a’ again, it's another ‘a’.
main() and nothing() are in different memory areas and the variables declared inside them have got nothing in common.
They just happen to have got the same name and value, as well as there could be two John Smith who have bought the same car model, without knowing each other.

That’s a ‘normal’ usage of functions: passing arguments by copy and returning values by copy.

If you want to avoid copies, you can use references:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Copied from Handy Andy's previous post
#include <iostream>

void nothing(int& a);

int main()
{
    int a{};
    nothing(a);
    int b = a + 2;
    std::cout << "i need... " << b << '\n';
    return 0;
}

void nothing(int& a) { a = 1; }


method 2: int a{} is nothing diferent from declare int a 1 more time

Look at the code: there’s no ‘again’: you declare ‘a’ in main(), but *not* inside nothing().
‘a’ is passed by reference. (The ‘cost’ of passing by reference is the same that passing by pointer.)
Declaring this way can really harm your software as doing so tend to generate high complexity which can lead to suffer from bugs and errors that are hard to detect unless you're using some program as checkmarx for that.
So that is your choice.
Good luck
Topic archived. No new replies allowed.