Newbie function confusion...

This is a discussion on Newbie function confusion... within the C++ Programming forums, part of the General Programming Boards category; Basicly, the main purpose of a function is to prevent repeated code, right? So, if you have a set of ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Unhappy Newbie function confusion...

    Basicly, the main purpose of a function is to prevent repeated code, right? So, if you have a set of code that your going to use again and again, you put the code into a function, and then just call the function. My problem is with variables - how do you use a common variable in four different functions? Like if your making an rpg, you would probably need the variable Players_Health. If you have four functions that use this variable, how do you have function1 recogonize the change of the Players_Health in function2 without making a new variable each time in each function?

    The only way I know to do this is to initilize and declare the variable in no function (at the very top) which is bad practice.

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Have you tried declaring parameters?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Even worse are functions that reference global variables, though. Have you used pointers? If not try this:

    Code:
    void double(int * health){
     *health *= 2;
    }
    void halve(int * health){
     if(*health)
     *health /= 2;
    }
    
    int main(){
    int some_health = 12;
    cout << "Health: " << some_health << endl;
    double(&health);
    cout << "Doubled: " <<  some_health << endl;
    halve(&health);
    cout << "Halved: " << some_health << endl;
    cin.get();
    return 0;
    }
    You could use references too which allow you to make permanant changes to the variable but using non-pointer syntax:

    Code:
    void double(int& health){
     health *= 2;
    }
    void halve(int& health){
     if(health)
     health /= 2;
    }
    
    int main(){
    int some_health = 12;
    cout << "Health: " << some_health << endl;
    double(health);
    cout << "Doubled: " <<  some_health << endl;
    halve(health);
    cout << "Halved: " << some_health << endl;
    cin.get();
    return 0;
    }

    Hope that helps...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Smile

    Oh yeah...reference parameters! Thx sebastiani

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Wait a sec...

    I have a major function, void Start();. I call it many times because it is like the main part of my program. Well, once I'm close to having 50+ variables Start() uses, I don't want each one to be a reference parameter. So how do I make them reference variables inside Start() without initilizing them, so that when i can call it, i can just simply say Start()?

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I would group the 50+ variables into a class or classes, and then pass the class by reference.

    If you can't figure any way to group the 50+ variables into a class/es, then I guess you could just store them in a array and then pass that to your function.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    No, an array wouldn't really work, and I'm not famililar with classes. Basicly want I want to do something that has the same effect as:

    void Start (int &a, int &b, int &c, int &d, int &e....);

    without having anything in the parenthesis so that it would look like:

    void Start ();

    How can I do this?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Make them global then. But remember: it is a very bad practice. Have you ever used a struct? A class is the same thing. And, assuming you are using C++, you can add methods to the struct.

    Code:
    typedef struct Data {
    int a;
    int b;
    int c;
    //...etc
    void Start(){
    a = b;
    b = c;
    }
    };
    
    
    int main(){
    Data data;
    data.Start();
    return 0;
    }

  9. #9
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I wouldn't trust Seb...

    He is old...

  10. #10
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by funkydude9

    without having anything in the parenthesis so that it would look like:

    void Start ();

    How can I do this?
    If you really don't want to pass any parameters you could do

    Code:
    #include//whatever
    
    int a;
    int b ;
    void Start(){
         //you can use a and b in here
    
    }
    
    int main(){
    
        //you can also use a and b in here
    
    }
    but this is bad progamming practice because it can lead to hard to catch side-effects,

    you should start to learn how to use classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21