Thread: Return / Exit Issue

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Return / Exit Issue

    Hi all,
    This is my first post here! I have two questions:

    1. I'm learning C by using 'Sams Teach yourself C for Linux Programming in 21 days'. I have finished the first week and have difficulties with the end-of-week programme. My problem is with a function called 'display()'. It is a void function so there is no return statement. But yet, I get the following error message from GCC:

    'in function "disply()"'
    'use "return" to return in void function'

    The message is very similiar to the above one but not exactly the same. Can anyone help?

    2. If I want to display the following message and then immediately quit the programme when I'm not in the main function
    what do I do?

    ' if(cont == NO)
    {
    printf("Programme aborted by user!");
    /* what's the command for exiting? It goes here */
    }
    Thanx
    Rhodium

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    You can use return to exit a void function, it will just not return a value to the calling function :
    Code:
    void testFunc()
    {
     // do stuff
     if ( ????? )
     {
       return;
     }
      // do stuff
    }
    This is usefull if you want to exit a function under certain conditions.

    To exit a program from a function you can use exit(exitcode) :
    Code:
    exit(0); // exits with error code of 0

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Lightbulb Thanx

    Thanx,
    I'll try that but I think I've already tried 'return;' with nothing in front of it but it doesn't hurt to try again...
    Thanx
    Rhodium

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    11
    Use return(); for type void functions. Sometimes picky compilers make you do that.

    -Will

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by C-Duddley
    Use return(); for type void functions. Sometimes picky compilers make you do that.

    -Will
    nope nope nope wrong wrong wrong nope nope nope wrong wrong wrong nope nope nope wrong wrong wrong.

    anyway Rhodium, give me the EXACT WORDS of your error message
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM