C++ - what's the best way to initialize a variable in one function/file and then use it in main()/another file? -


in c++, let's need assign variable , want outside of main() code clearer, want use variable operations inside main() or function. example have:

int main() { int = 10; int b = 20; somefunction(a,b); } 

and want have this:

void init() { int = 10; int b = 20; }  int main() { somefunction(a,b); } 

but compiler , b undeclared in scope of main(). declare them global variables there better way solve problem , read global variables not great in long run. don't want use classes. guys propose?

use structures:

struct data {     int x;     int y; };  data init() {     data ret;     ret.x = 2;     ret.y = 5;     return ret; }  int main() {     data v = init();     somefunction(v.x, v.y); //or change function , pass there structure     return 0; } 

if don't want use struct can pass values init function reference. in opinion first version better.

void init(int &a, int &b) {     = 5;     b = 6; }  int main() {     int a, b;     init(a, b);     return 0; }