Thread: Newbie variable advice

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Newbie variable advice

    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.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You need all 12 of them in the other function? Pass whatever you need as parameters by reference.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Consider putting the data into a struct or class and passing that to the functions, as swoopy said.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    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.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 182
    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?
    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.

    Quote Originally Posted by 182
    I was also wondering if variables can be used in a switch statement by using code such as a.variable as well?
    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.

    Quote Originally Posted by 182
    and does a struct have to be declared in a certain place?
    The struct has to be declared before main. Objects can be defined anywhere plain data can.

    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Ž

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    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.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, members could be the condition. They're just plain data.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks
    Last edited by 182; 02-15-2006 at 04:11 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

    am I best just making them all global variables or should I pass them all as parameters?
    You should never declare a variable as a global variable.
    Last edited by 7stud; 02-15-2006 at 04:10 PM.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    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?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-21-2009, 02:27 PM
  2. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  3. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  4. Newbie function confusion...
    By funkydude9 in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2002, 02:15 AM
  5. newbie needs advice!
    By bluenoser in forum C Programming
    Replies: 24
    Last Post: 10-16-2002, 07:28 PM