Thread: A question from a newbie regarding nested function

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16

    A question from a newbie regarding nested function

    Hello there,
    I’m a brand-new student in programming arena. Actually I’m a student of other discipline and I decided to learn C language. To do so I’m currently using the book “Teach yourself C – Herbert Schildt” and a compiler “Borland C++ 5.02”. I’m just following the book as directed, but while compiling the following program, the compiler gives the message “(5, 16): Call to undefined function ‘get_sqr’. I want some help from you guys and tell me where the mistake in this program is.


    #include "stdio.h"
    main()
    {
    int sqr;
    sqr = get_sqr();

    printf("square: %d", sqr);
    }

    get_sqr()
    {
    int num;

    printf("enter a number: ");
    scanf("%d", &num);
    return(num*num);
    }
    Last edited by Sharifhs; 08-09-2010 at 05:17 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Sharifhs View Post
    Hello there,
    I’m a brand-new student in programming arena. Actually I’m a student of other discipline and I decided to learn C language. To do so I’m currently using the book “Teach yourself C – Herbert Schildt” and a compiler “Borland C++ 5.02”. I’m just following the book as directed, but while compiling the following program, the compiler gives the message “(5, 16): Call to undefined function ‘get_sqr’. I want some help from you guys and tell me where the mistake in this program is.
    Code:
    #include "stdio.h"
    
    int get_sqr(void);   
    
    int main()
    {
       int sqr;
       sqr = get_sqr();
    
       printf("square: %d", sqr);
    
       return 0;
    }
    
    int get_sqr(void)
    {
       int num;
    
       printf("enter a number: ");
       scanf("%d", &num);
       return(num*num);
    }
    1) change "main" to "int main" and add a return 0 onto the end of int main().
    2) get_sqr() has no return type specified - which with Borland, means it will expect an int by default.

    But you should specify it with a prototype, before your main function, as shown above.

    For the code tags to work, just use ONE pair of them, and paste your code between the tags.

    And

    * Welcome to the Forum! *


    Edit: added the voids!
    Last edited by Adak; 08-09-2010 at 06:43 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Thank Adak for your quick reply but it's not yet working or may be I couldn't understand you what you wanted to mean. Could you explain it again?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    There's no nested function in C.
    C compiler don't know the return type of function get_sqr(). and assumes that your function returns int type.
    You can either define the function or prototype function before calling it.
    Code:
    int get_sqr(void);  
    // then call it.
    or 
    int get_sqr(void)
    {
       ...
    } 
    // then call it

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Thanks a bunch Adak...it works!!!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good eye as always, Bayint!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would dump that compiler and get a better one. Mingw for windows is a good one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Thanks Bayint Naung and Elysia for leaving useful comments

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Quote Originally Posted by Adak View Post
    But you should specify it with a prototype, before your main function, as shown above.

    Dear Adak, please forgive my ignorance, but just for understanding, could you tell me what is a prototype and where is it used in this program?
    Last edited by Sharifhs; 08-09-2010 at 10:17 AM.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That Bayint is EVERYWHERE!

    Code:
    #include "stdio.h"
    
    int get_sqr(void);   /* <----the function definition or prototype */
    
    int main()
    {
       int sqr;
       sqr = get_sqr();
    
       printf("square: %d", sqr);
    
       return 0;
    }
    
    int get_sqr(void)  /* <---the function declaration */
    {
       int num;
    
       printf("enter a number: ");
       scanf("%d", &num);
       return(num*num);
    }
    You can get away without a function definition IF the function is listed in your program, before the main() function. It makes for nasty looking code to study, imo, and your coding improves when you have a real prototype or definition for each function.

    The easy way to get your function definitions right, is to just copy the function declaration in your editor. Then move to the area above main(), and paste it into place. Now remove the { from the end of the line (if present), and add a semi-colon ; right at the end of the line.

    The function definition MUST match the function declaration, unless your compiler accepts some default values and your function happens to match those default values.
    Don't rely on default values for function definitions or function declarations - be specific.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Thanks again Adak. I'm trying to understand things you've posted.
    Thanks Bayint too

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, Adak mixed up the terms: what Adak calls the "the function definition or prototype" is actually "the function declaration or prototype", and what Adak calls "the function declaration" is actually "the function definition". (But then function definitions are function declarations, which is why a function definition placed above the main function suffices for you to call the function from the main function.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Strictly,
    You can get away without a function definition IF the function is listed in your program, before the main() function
    It should read as before the caller(use of. in case of function pointer function.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Quote Originally Posted by Sharifhs View Post
    Dear Adak, please forgive my ignorance, but just for understanding, could you tell me what is a prototype and where is it used in this program?
    A prototype is setting the interface for the function before it's used. Basically it's saying what type of value the function receives(arguments), what value it returns and of course the name of the function.

    As its easier to explain in code:

    Code:
    #include<stdio.h>
    int calculator(int, int); // Prototype - returntype name(argument/s)
    int main()
    {
        printf("Please insert two factors to multiply.\n The first:");
        int factorOne, factorTwo;
        scanf("d%", &factorOne);
        printf("The second: ");
        scanf("d%", &factorTwo);
        int product = calculator(factorOne, factorTwo); // Functioncall
        printf("The product of the factors multiplied is: d%", &product);
        getchar();
        return 0;
    }
    int calculator(int factorOne, int factorTwo)
    {
        return factorOne * factorTwo;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 09:21 PM

Tags for this Thread