#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0

    Program with self-created function


    The book I'm reading from first had me do a program with a function prototype (in a "while" loop) and then told me to modify it so that the code from the start of the loop's variable declaration and put into a new function that would be called by the "main()" function.

    The original code looks like this:
    Code:
    #include <stdio.h>
    
    void prompt();  /* function prototype */
    
    int main()
    {
        int loop;
        char input[32];
        loop=0;
        while(loop<5)
        {
            prompt();
            fgets(input,31,stdin);
            loop=loop+1;
        }
        return(0);
    }
    
    /* Display prompt */
    
    void prompt()
    {
        printf("C:\\DOS> ");
    }
    The modified code I'm having problems with is like this:
    Code:
    #include <stdio.h>
    
    void prompt();  /* function prototype */
    
    int main()
    {
        int busy()
        {
            int loop;
            char input[32];
            loop=0;
            while(loop<5)
            {
                prompt();
                fgets(input,31,stdin);
                loop=loop+1;
            }
            return(0);
        }
    }
    
    /* Display prompt */
    
    void prompt()
    {
        printf("C:\\DOS> ");
    }
    And the specific problem my compiler is giving me is a warning that reads as such:
    C:\Users\Osman\Documents\BegC4D\ex1002\main.c|20|warning: control reaches end of non-void function [-Wreturn-type]|
    Any help would be apprecated.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    It is very simple: in C, you cannot embed one function inside of another. The warning only addresses the fact that you did not return anything from main(). I am also at a loss to explain why it didn't say anything about your having committed such an egergious violation of C program structure. I can only guess that the compiler was at a complete loss to understand what you were trying to say and just ignored the presence of busy().

    You must move the declaration of the new function, busy, outside of main() as you had done with the function, prompt. Be sure to prototype it as well so that you can call it from within main().
    Last edited by dwise1_aol; January 3rd, 2014 at 06:16 PM.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    GCC supports nested functions.

    You need to compile with the pedantic option to get a warning.
    Code:
    $ gcc --std=c99 --pedantic foo.c
    foo.c: In function ‘main’:
    foo.c:7:5: warning: ISO C forbids nested functions [-pedantic]
    The nested function isn't the problem, it's that you don't call the function within main at all (so nothing actually happens).

    Comments on this post

    • dwise1_aol agrees : That is absolutely literally the first that I ever heard of such a thing in C -- we did it all the time in Pascal, but not in C. So gcc plays fast and loose with the rules? Naïve little me!
    • 2teez agrees : Agrees with the nested function point
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    Originally Posted by dwise1_aol
    It is very simple: in C, you cannot embed one function inside of another. The warning only addresses the fact that you did not return anything from main(). I am also at a loss to explain why it didn't say anything about your having committed such an egergious violation of C program structure. I can only guess that the compiler was at a complete loss to understand what you were trying to say and just ignored the presence of busy().

    You must move the declaration of the new function, busy, outside of main() as you had done with the function, prompt. Be sure to prototype it as well so that you can call it from within main().
    How is it now?

    Code:
    #include <stdio.h>
    
    void prompt();  /* function prototype */
    void busy();
    
    int main()
    {
        int loop;
        char input[32];
        loop=0;
        while(loop<5)
        {
            prompt();
            fgets(input,31,stdin);
            loop=loop+1;
        }
        return(0);
    }
    
    void busy()
    {
       int loop;
        char input[32];
        loop=0;
        while(loop<5)
        {
            prompt();
            fgets(input,31,stdin);
            loop=loop+1;
        }
        return;
    }
    
    /* Display prompt */
    
    void prompt()
    {
        printf("C:\\DOS> ");
    }
    Should I have done it in a way that would have allowed me to return a value in "return"? And if so, how? Sorry if this is a dumb question.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,704
    Rep Power
    480
    Your busy function is unused. The instructions are to me incomprehensible. Proceed to the next exercise.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    I'll move on when I've understood this as much as I can. Anyway, I tried to modify the code, but it still doesn't work like I want it to.

    Code:
    #include <stdio.h>
    
    void busy();
    
    int main()
    {
        void busy()
        {
            int loop;
            char input[32];
            loop=0;
            while(loop<5)
            {
                busy();
                fgets(input,31,stdin);
                loop=loop+1;
            }
        return;
        }
    return(0);
    }
    
    /* Display prompt */
    
    void busy()
    {
        printf("C:\\DOS> ");
    }
    Okay, so what's wrong with it now?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by DragonOsman2
    I'll move on when I've understood this as much as I can. Anyway, I tried to modify the code, but it still doesn't work like I want it to.
    From your previous post all you needed to have done was to call your
    Code:
    busy();
    function. Then return 0 for the main function. Then you are home and dry.

    The below works for me. But also note that since the busy function nested in the main function, you can't call it outside the main function.
    Code:
    ...
    int main()
    {
        int busy()
        {
            int loop;
            char input[32];
            loop=0;
            while(loop<5)
            {
                prompt();
                fgets(input,31,stdin);
                loop=loop+1;
            }
            return(0);
        }
        
        busy(); // call the busy function NOTE HERE
        
        return 0;
    }
    ...
    You can put in the other parts.
    Hope this helps
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    Yeah, that should help. I'll try it. Thanks a bunch.

    Edit: Yeah, it helped.

IMN logo majestic logo threadwatch logo seochat tools logo