noob calling value in func help

This is a discussion on noob calling value in func help within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a complete noob at programming and thought I would combine 2 lessons that I have learnt. 1. ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    noob calling value in func help

    Hi, I am a complete noob at programming and thought I would combine 2 lessons that I have learnt.
    1. assigning a number/value to a letter.
    2. function returning a value.

    I am trying to call the function testing which holds 'y' declared as the number '2', but all that comes up on the 2nd line is the number '35'.
    I am using bloodshed dev-c++.

    Thanks for any info you can give me.... usually I find the problem after a while.


    Code:
    #include <iostream.h>
    using namespace std;
    
    void testing();
    
    int main()
    {
        int x;
        x = 1023;
        
        cout << "this program prints the value of x: ";
        cout << x;
        cout << "\n";
        
        testing();
        cout << "This line will print the value of y:";
        cout << y;
        cout << "\n";
        
        system("PAUSE");
        
        return 0;
    }
    
    int testing()
        {
        int y;
        y = 2;       
    }

    Edit: I would really like to call the value from the other func... not the main() if possible.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    You declared 'y' within the scope of a function, when the function scope ends the variable is destroyed. You'll want to declare 'y' in the global scope and remove the declaration within the function testing() to make it work that way. Preferably you should pass it by reference or as a pointer to the function testing(int &y) and then assign a value to it within the function.

    Learn about variable scope here; http://www.cplusplus.com/doc/tutorial/variables/
    (Scroll down to the heading 'Scope of variables' and read.)

    Learn about pointers and references here; http://www.cplusplus.com/doc/tutorial/pointers/

    ... read the information at those links. >:]
    Last edited by syneii; 12-07-2010 at 08:23 AM.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    oh cool, thanks heaps dude!
    I'll check it out.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Thanks for the 'Variables' website, I was able to declare 'y = 2' globally and call a modification of the variable from another function, from what I read.

    For any other noobs who might be interested, this it how it worked.

    Code:
    #include <iostream.h>
    using namespace std;
    
    
    int y = 2;
    
    void myfunc();
    
    int main()
    {
        int x;
        x = 1023;
        
        
        cout << "this program prints the value of x: ";
        cout << x;
        cout << "\n";
        
        
        cout << "This line will print the value of y: ";
        cout << y;
        cout << "\n";
        
        myfunc();
        cout << "\n";
        
        system("PAUSE");
        
        return 0;
    }
    
    
    void myfunc()
    {
         cout << "This line will print out the value of line y+1: ";
         cout << y + 1;
         }
    Code:
    this program prints the value of x: 1023
    This line will print the value of y: 2
    This line will print out the value of line y+1: 3
    Press any key to continue...

    Thanks again dude.
    Last edited by Sinamp; 12-07-2010 at 09:03 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Sinamp, avoid the use of global variables. For example, your program could be written as:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int myfunc(int y);
    
    int main()
    {
        int y = 2;
        int x = 1023;
    
        cout << "this program prints the value of x: " << x << "\n";
        cout << "This line will print the value of y: " << y << "\n";
        cout << "This line will print out the value of line y+1: " << myfunc(y) << "\n";
    
        system("PAUSE");
    
        return 0;
    }
    
    int myfunc(int y)
    {
        return y + 1;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Ah, I see what you mean.
    Using global variables in programming like that could run me into a lot of problems later on.
    Looks like the 'main()' function is supposed to carry the important details like the variables (eg. core code), and the other function's only jobs are to set derivations from the core code.... from what I can gather.
    Thanks Laserlight

    P.S. I am up to local, formal and global parameters in the book I'm using.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 02:12 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM

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