Retrieving a varible from a different function

This is a discussion on Retrieving a varible from a different function within the C++ Programming forums, part of the General Programming Boards category; I tried searching for this but didnt get any results. What I would like to do is have "function a" ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    Retrieving a varible from a different function

    I tried searching for this but didnt get any results. What I would like to do is have "function a" use "variable b" which is stored in "function c". This shouldnt be terribly complicated, but I am having trouble devising a way to do this.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Pass as a parameter, or return it as a result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What I would like to do is have "function a" use "variable b" which is stored in "function c".
    If "function a" needs "variable b" then why is it stored in "function c"? Wouldn't it make more sense for both function a and c to be member functions in a class where variable b is a data member? It sounds to me like your design needs a refit rather than a kludge.
    My best code is written with the delete key.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    either have function a return variable b, or have variable b passed to function c as a reference so that any change made in function a would reflect in function c. Of course varialbe b would have to be defined in the global scope of both functions.

    or it could all be one class, where variable B is a private/public member that both function a and c could change

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Oh classes, I figure I would have to use them sooner or later, I will give a crack at it and if I have any more question I shall post my code. Thankyou.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    or a struct.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Alright, when you declare a public variable (like int a), how do you use that variable in seperate function? Also how might a struct work with this?

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    by the way, when I try, I get the error: 12 new types may not be defined in a return type

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Read the tutorials on this website about functions lilrayray... pretty please
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    I beleive I did and they didnt seem to conver more then their basic use and how to declare them; that is unless i am missing somethign?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You saw this? http://www.cprogramming.com/tutorial/lesson4.html

    error: new types may not be defined in a return type
    I think this means that you have as the return type of a function "class foo", and class foo has not been declared.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    I am hiving trouble with classes. Once you declared and have written a function that is a member of a class, how do you use that function with the main function? for example:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class t{
          public:
                 void test();
                 int a;
    };
    int main(void)
    {
        test();
        cin.get();
    }
    
    void t::test(){
         a = 10;
         cout<<a;
    }
    Also, what are constructors and destructors? do I need them?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Hint: static variables/functions.

    A static variable is a variable that exists only once for every instance of the class; a static method can access such static variables.

    Code:
    class c {
    private:
        static int x;
    
    public:
        int get_x() { return x; }
        void set_x(int t) { x = t; }
    };
    
    void function() {
        c::set_x(5);
        cout << c::get_x();
    }
    or
    Code:
    class c {
    public:
        static int x;
    };
    
    void function() {
        c::x = 5;
        cout << c::x;
    }
    You could use a structure instead of a class for the second example, which would allow you to drop the "public:".

    [edit]
    Constructors and destructors are special methods called every time an instance of the inclosing class is created or destoyed, respectively. You don't need to use them for a class that only has static variables.
    [/edit]

    [edit=2]
    To give an initial value to a static variable, try this:
    Code:
    class c {
    public:
        static int x;
    };
    
    int c::x = 5;
    [/edit]
    Last edited by dwks; 08-24-2006 at 03:29 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    i take it you just prefer to ask us intead of reading the tutorials right?

    "What the heck! Why should I do all the work. Let someone answer my doubts while I sit here calmly waiting. Searching is for those nerds who like to know everything"


    A member function is used with the help of the member access operator.

    Code:
    t myObject;
    myObject.test(); // <-- this is how you use it
    Constructors are ran when you instantiate an object of your class. They are usually used to initialize data membrs and do some administrative tasks.

    Destructors are ran when the object of the class dies (goes out of scope or is explicitely deleted). Usually they are used to do some cleaning up.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    For something as simple as a variable that can be accessed from two functions, you probably don't need a class. As mentioned, pass arguments:
    Code:
    void print(int x) {
        cout << x;
    }
    Or return values:
    Code:
    void get() {
        int x;
        cin >> x;
        return x;
    }
    Or pass references to see your changes:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void add(int x) {
        x ++;
    }
    
    void addref(int &x) {
        x ++;
    }
    
    int main(void) {
        int x = 5;
    
        cout << x << endl;
        add(x);
        cout << x << endl;
        addref(x);
        cout << x << endl;
    
        return 0;
    }
    Output:
    Code:
    5
    5
    6
    As you can see, if you pass a regular variable, the function gets a local copy of it and can do what it wants to it. It you want the changes to be reflected, return the variable after you've modified it, or pass a reference/pointer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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