Thread: Returning using a void??

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Returning using a void??

    Hello!

    So, I am completely stuck on my last HW problem. This is the problem it gives to me:

    Change the printNumber method :

    1. Change the code INSIDE the method to have it RETURN the value.
    2. In the main(), create an int value favNum
    3. In main(), call the method and have the value returned assigned to the variable favNum
    4. In the main(), display favNum

    I'm sure I would be fine on the rest if I could only figure out what exactly he's talking about for the first one? How do I return from a void? And I am guessing by 'the value', he means return 23? I don't know why I'm so confused, I did just fine on all the other 5 problems.

    This is what I have.

    Code:
    # include <stdio.h>
    
            /*all prototypes here*/
            void printColor();
            void printGreek();
            void printFood();
            void printSport();
            void printNumber();
            void printSubject();
            void printPet();
            void printCar();
            void printTeam();
    
            int main()
            {
                    /* make all calls from here!!! */
                    int favNum;
    
    
                    return 0;
            }
    
            /* all methods here */
            void printColor() { printf("Blue\n"); }
            void printGreek() { printf("Pi\n"); }
            void printFood() { printf("Pizza\n"); }
            void printSport() { printf("Hockey\n"); }
            void printNumber() { printf("23\n"); }
            void printSubject() { printf("English\n"); }
            void printPet() { printf("Cat\n"); }
            void printCar() { printf("REG 123 Blazer\n"); }
            void printTeam() { printf("Browns\n"); }
    Thank you for any help!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Chinnie15 View Post
    Change the printNumber method :

    [LIST=1][*]Change the code INSIDE the method to have it RETURN the value.

    I'm sure I would be fine on the rest if I could only figure out what exactly he's talking about for the first one?
    Currently, printNumber prints a number. It does not return any value, nor can it, because it is of type void.

    So the instruction is a little deceptive: in addition to changing the code inside the function, you will also have to change its type. Ie, the prototype declaration would be:

    Code:
            int printNumber();
    Hopefully you will understand why, and that will be a big clue about how to do the rest.

    Nb. There is no other possibility here; a void function cannot return anything, and the instructions you were given confuse this, unfortunately.
    Last edited by MK27; 10-27-2011 at 09:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thank you! That's what I thought I had to do, but the way it was worded I thought it would be wrong. I've got it now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void returning functions.
    By admin in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2011, 11:31 PM
  2. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  3. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  4. function returning void
    By studentc in forum C Programming
    Replies: 14
    Last Post: 05-13-2004, 04:55 PM
  5. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM