What are global variables exactly??

Pages: 12
As long as you store them in some variable in main when you return from the function, or if you pass by pointer or reference. Here is an example of both:

pointer/reference

void DoSomething(int* firstVar, double& SecondVar)
{
*firstVar = 0;

SecondVar = 0;

return;
}

returning:

int DoSomething( int firstVar)
{
//do something
return firstVar;
}
Actually, in my example I broke the rules set by your teacher a little bit. The struct X is in fact global it that case (but I don't think your teacher would complain). To make it a proper local variable you have to declare it inside main() and then pass it as argument (by reference):

1
2
3
4
5
int main()
{
YourStruct X;
Get_Item(X);
}


EDIT: or you can return a struct from the function (no references needed which is probably your task):

1
2
3
4
5
6
7
8
9
10
11
YourStruct Get_Item(/* ... */)
{
YourStruct Temp;
// code here
return (Temp);
}

int main()
{
YourStruct X = Get_Item(/* ... */);
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12