hey guys, I have a function that has a lot of local variables around 12 and I need these in another function am I best just making them all global variables or should I pass them all as parameters?
Thanks.
hey guys, I have a function that has a lot of local variables around 12 and I need these in another function am I best just making them all global variables or should I pass them all as parameters?
Thanks.
You need all 12 of them in the other function? Pass whatever you need as parameters by reference.
Sent from my iPadŽ
Yeah its a bit of a weird program but I need all 12 of them but by the end of that program that number could even be as much as 50 or more can this amount of variables be passed like the following in one function call?
functionCall(s,a,t,y,u..etc);
(I only need them in another fuction so they can be displayed with other data at the end after all the calculations are done in the first function. So des this also mean that I can pass by value?)
Last edited by 182; 02-15-2006 at 03:35 PM.
It sounds like you either need:
(A) An array if any of the variables represent the same thing or
(B) A class so the functions can share the same variables.
But you can certainly pass them as parameters if you want.
Consider putting the data into a struct or class and passing that to the functions, as swoopy said.
Sent from my iPadŽ
I may use a struct thanks, I have a few struct questions though instead of passing a variables as a parameter could it be accessed in any function by using code such as a.variable or would I have to pass the struct using functionCall(struct a)?
I was also wondering if variables can be used in a switch statement by using code such as a.variable as well? and does a struct have to be declared in a certain place?
Sorry for these annoying newbie questions but im quite new to c++ and these concepts are all new to me.
Last edited by 182; 02-15-2006 at 03:57 PM.
No it can't because your object will be local to where it's defined. So if you create an object in main, it has to be passed to the function as a parameter.Originally Posted by 182
How do you mean "in switch statements"? Can they be used as your switch's condition? Likely not. You can certainly use them in the cases of your switch statement so long as they remain in the same scope they're declared in.Originally Posted by 182
The struct has to be declared before main. Objects can be defined anywhere plain data can.Originally Posted by 182
Code:struct data { // All data in structs are public by default int number; char letter; string word; }; // Don't forget the semicolon data Foo; // A legal global declaration void func(data, data &); // Passes the struct as a parameter int main() { data Bar; // Declared local to main func(Bar, Bar); // Passes the struct to the function // Bar.number equals 8, right here Bar.number = 5; return 0; } void func(data Baz, data &Buz) { // Baz is local to the function and will disappear with the function, any changes to Buz will change Bar in main Baz.number = 6; Buz.number = 8; }
Last edited by SlyMaelstrom; 02-15-2006 at 04:09 PM.
Sent from my iPadŽ
Thanks for that, I think a struct would be the best option for me. When I was talking about the switch statement one of the members of the struct would have to be the condition and some other would be used in the cases. Will this then cause a problem for the switchs condition?
Thanks again for your previous post its a huge help.
Yes, members could be the condition. They're just plain data.
Sent from my iPadŽ
Thanks
Last edited by 182; 02-15-2006 at 04:11 PM.
If you have a struct called A, and one of its members is a variable called 'num', and num is an int type, and num is equal to 10, then A.num can be used any place that 10 can be used in your code. A.num and 10 are identical as far as your program is concerned.
You should never declare a variable as a global variable.am I best just making them all global variables or should I pass them all as parameters?
Last edited by 7stud; 02-15-2006 at 04:10 PM.
ive noticed in your example that the members of the struct are not initlized can they be or do they have to be initlized once in a function?
The function that initializes them is called a constructor. You can have a constructor for a struct just like you have for a class. You can also just explicitly initialize the values one at a time when you create the struct if you'd prefer.