Thread: Function

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Question Function

    Code:
    int random()
    {
        const int MAX_NUMBER = 7;
        srand(GetTickCount());
        const int mn = static_cast <double> (rand()) / RAND_MAX * MAX_NUMBER +1;
        return mn;
    }
    
    int main()
    {
        random();
        cout << mn;
    }
    It says: "Undefined symbol `mn` in function main()"
    Why? How can I avoid that error?
    I can't find it in my book.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    well -- where to you see mn defined?
    Why? How can I avoid that error?

    define it. The variable in random() cannot be used in any other function.
    Code:
    int mn;
    mn = random();
    Last edited by Ancient Dragon; 02-22-2006 at 12:18 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Two functions cannot see what's inside the other. main() is one function and random() is another function. The variables you declare in random() are local to that function, and when the function ends, they are destroyed. So, when you call random(), the function executes, and then mn is destroyed. random() does return a value, but you don't assign that value to any variable, so it is discarded.

  4. #4
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    wouldn't something like this work?
    Code:
    int random()
    {
       //...
    }
    
    int main()
    {
       cout<<int random();
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    No....try it.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by rwmarsh
    wouldn't something like this work?
    Code:
    int random()
    {
       //...
    }
    
    int main()
    {
       cout<<int random();
    }
    No, but this might
    Code:
    int random()
    {
       //...
    }
    
    int main()
    {
       cout<< random();
    }
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM