access data outside a function

Feb 11, 2013 at 2:18am
I was wondering what the best way was to access data outside the function it was created in. I have the user input a data in a custom namespace function, and now im trying to access all those data points in a different function. any advice? I can post the code, but it's a little long...
Feb 11, 2013 at 3:00am
You might try initializing a global variable before you do "int main()", then, in the other function, set the value you want to have access to elsewhere equal to the global variable.

Be careful though, consider if you need it to be initialized again, besides at the top of the program.
Feb 11, 2013 at 3:18am
Tried that global variable thing,

1
2
3
4
5
6

namespace functions
{
	//my .h file
	int collectInput();	
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int functions::collectInput()
{
    // my functions.cpp file
    int a1,b1,c1,a2,b2,c2,x = 0;
    
	cout<<"please enter three numbers seperated by spaces. these will be the A,B,C values\n"
	"for your first quadratic equation."<<endl;
	cin>>a1;
	cin>>b1;
	cin>>c1;
	if(!cin.good())
	{
	cout<<"Bad number, please try again."<<endl;
	exit;
	}
	cout<<"please enter three numbers seperated by spaces. these will be the A,B,C values\n"
	"for your SECOND quadratic equation."<<endl;
	cin>>a2;
	cin>>b2;
	cin>>c2;
	if(!cin.good())
	{
	cout<<"Bad number, please try again"<<endl;
	exit;
	}
	cout<<"enter an 'x' value for both quadratic equations"<<endl;
	cin>>x;
	if(!cin.good())
	{
	cout<<"Bad number, please try again"<<endl;
	exit;
	}

}


all i really want to do for right now, is access a1 b1 c1...etc, outside this function....
Feb 12, 2013 at 1:52am
Does anyone else have any insight? I'm just a little lost.
Feb 12, 2013 at 2:09am
1
2
3
4
5
int functions()
{
      code... place a1, b1 etc. into an array
      return array;
}
Feb 12, 2013 at 10:37am
Firstly, I'd recommend avoiding global variables. While they can occasionally be useful, when you move on to larger projects they can cause all kinds of problems, so I would strongly advise you DON'T get into the habit of using them.

If you want to pass data from your function out to the calling code, the principal ways are:

1) Return values. A function can return a single object. This can be a single value, or it can be an object such as a vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This function returns the sum if a + b
int Sum(int a, int b)
{
  int theSum = a + b;
  return (theSum );
}

// It can be called like this:

void AnotherFunc(void)
{
  int mySum = Sum(3, 5);

  // This will print the value 8
  std::cout << mySum << std::endl;
}


2) If you specify your function arguments to be references, using the & symbol, then if the function makes changes to their values, then the calling code will see the changed values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void IncrementValues(int& a, int& b)
{
  a = a + 1;
  b = b + 1;
}

void AnotherFunc(void)
{
  int a = 2;
  int b = 3;

  IncrementValue(a, b);

  // The following will print the values 3 and 4 for a and b
  std::cout << "a is " << a << " and b is " << b << std::endl;
}


3) If you specify your function parameters as pointers, then any changes the function makes to the values pointed to by those arguments will also be visible to the calling code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void IncrementValues(int* a, int* b)
{
  *a = *a + 1;
  *b = *b + 1;
}

void AnotherFunc(void)
{
  int a = 2;
  int b = 3;

  IncrementValue(&a, &b);

  // The following will print the values 3 and 4 for a and b
  std::cout << "a is " << a << " and b is " << b << std::endl;
}


Edit: Using functions is one of the very basic foundation skills of writing in C or C++. If you don't understand it, you really need to go back to your texts and learn about them properly. You won't get far in programming without them.
Last edited on Feb 12, 2013 at 10:38am
Feb 13, 2013 at 2:48am
I never got the hang of pointers...
Feb 13, 2013 at 8:51am
I never got the hang of pointers...


A pointer is simply a single number that identifies a memory address. The type of the pointer (e.g. "pointer to int", "pointer to string") identifies what sort of data is stored at that address.
Feb 14, 2013 at 12:27am
kewl
Feb 14, 2013 at 2:53am
Just declare and initialize each outside of the function where they are used.
Topic archived. No new replies allowed.