Thread: a question about functions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Question a question about functions

    wats the use of function prototype , regarding that we cannot add it to the code and the prog runs perfect ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Function prototypes exist for 2 reasons...

    1) C can't compile anything it doesn't already know about. Thus it can't compile a call to a function it hasn't seen yet. Since we can't always order functions that way (eg where Foo calls Bar and Bar also calls Foo) Prototypes provide a "forward reference" to allow the the compiler to work with out of order definitions.

    2) In program headers, which are included into other programs, prototypes allow the compiler to work with functions that exist in other pages of source code.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    If you're working on a project with multiple source files, you need to write a header file and put function prototypes in there, then include the header in each source file, so that functions are aware that other functions exist. You also need to declare prototypes if you wrote other functions after the main function.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by CommonTater View Post
    1) C can't compile anything it doesn't already know about. Thus it can't compile a call to a function it hasn't seen yet. Since we can't always order functions that way (eg where Foo calls Bar and Bar also calls Foo) Prototypes provide a "forward reference" to allow the the compiler to work with out of order definitions.
    That is not entirely correct.

    GCC, f.ex., will compile a function w/o a prototype -- a warning will be given but it will compile. What you will lose when you don't provide a function prototype is type checking when making a function call. The below code will compile, but run and crash because I used bad variable names and made a typo. No warnings will be given about incorrect argument type. If you uncomment the prototype, you'll get a warning.

    Code:
    #include <stdio.h>
    
    
    /* int msh_strlen(const char* string); */
    
    int main(void)
    {
      int s;
      char* S = "hello, world!";
    
      s = msh_strlen(s);
      printf("??? s = %d\n", s);
    
      return 0;
    }
    
    
    int msh_strlen(const char* string)
    {
      int i = 0;
    
      while (*string++)
      {
        ++i;
      }
    
      return i;
    }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by thekaiser View Post
    wats the use of function prototype , regarding that we cannot add it to the code and the prog runs perfect ?
    Othwise known as a "Forward declaration"; sure there are cases where they are not required, in which case I agree that they serve no purpose other than being documentary. However there are also other cases where a C program will not compile without them.

    FYI: Some other languages such as 'D' do not require function prototypes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by msh View Post
    That is not entirely correct.

    GCC, f.ex., will compile a function w/o a prototype
    IMO... that's a weakness in GCC and not proper behavior at all.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    IMO... that's a weakness in GCC and not proper behavior at all.
    IMO... that's a weakness of C prior to C99, though it is proper behaviour since it is um, C.
    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

  8. #8
    Novice
    Join Date
    Jul 2009
    Posts
    568
    GCC gives a warning, which everyone is free to ignore at their own peril.

    I would say that this is proper behavior, as I couldn't find anything in the standard that would mandate the existence of a function prototype (declaration), only that it can exist, and that it should agree with function definition.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by msh
    I would say that this is proper behavior, as I couldn't find anything in the standard that would mandate the existence of a function prototype (declaration), only that it can exist, and that it should agree with function definition.
    To be accurate, C99 does not require that a function's prototype be in scope before the function is called, but I believe that it does require that a function declaration that serves as a prototype, or the function definition, be in scope before the function is called.
    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

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by laserlight View Post
    To be accurate, C99 does not require that a function's prototype be in scope before the function is called, but I believe that it does require that a function declaration that serves as a prototype, or the function definition, be in scope before the function is called.
    I defer to you in this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by msh View Post
    That is not entirely correct.

    GCC, f.ex., will compile a function w/o a prototype -- a warning will be given but it will compile. What you will lose when you don't provide a function prototype is type checking when making a function call.
    ...
    Not entirely correct. Compiler will default function's arguments to type int, and I believe its return type is also int as opposed to void. You do not lose type checking. It's just that all subsequent type checking will be done assuming all arguments should be ints.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    thnx

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    thats what i mean we dont have function prototype ...

    [code]
    #include <stdio.h>
    #include <conio.h>

    int enter (void)
    {
    int x;
    printf(" enter an integer:");
    scanf("%d",&x);
    return x;
    }

    int cube (int z)
    {
    return (z*z*z);
    }

    int main()
    {
    clrscr;
    int y,f;
    y = enter();
    f=cube(y);
    printf("after cube:%d",f);
    while(!kbhit());
    return 0;
    }
    [\code]

    so u guys say that its wrong and i shouldnot do this again ?
    explain plz

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here, let me make this easy:

    Functions are tools. The return type tells what you get from the function. The function name is the name of the tool. The arguments are what the tool needs to work right.

    A function prototype is a description of the tool, telling you everything I mentioned in the paragraph above this, so that when you have the tool available, you know how to use it.

    A function definition is the actual details of how the tool works. Think of it as a detailed diagram showing everything about the tool itself.

    A function call is you using the tools.

    You don't need a short description of the tool (function prototype) if you have a detailed schematic of it (function definition).
    Code:
    void foo( void ); /* prototype - description of tool */
    
    int main( void )
    {
        foo( ); /* function call - using tool */
        return 0;
    }
    
    void foo( void ) /* function definition - schematic of tool */
    {
        printf( "hello world\n" );
    }
    If you move the function definition to the top, you can throw out the description of the tool, since you'll have the full tool diagram available:
    Code:
    void foo( void ) /* function definition - schematic of tool */
    {
        printf( "hello world\n" );
    }
    
    int main( void )
    {
        foo( ); /* function call - using tool */
        return 0;
    }
    See?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    ya thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about redefining functions in derived class
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2009, 11:48 AM
  2. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  3. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  4. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  5. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM