Thread: prototype

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    prototype

    When people give a prototype,

    Code:
    ie/ rval foo(Int32 val, Int32 *val2)
    Is it supposed to be what I am passing into the function or what the first line of the function looks like?

    like is it

    Code:
    >>function foo(Int32 val, Int32 *val2){
    ...
    }

    or
    Code:
    int main(void){
    >>foo(Int32 val, Int32 *val2)
    }
    Thanks!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > ...what the first line of the function looks like?
    By that you mean the function definition? Then yes, that's correct.

    Code:
    int foo(int val, int * val2);
    
    int main(void)
    {
        int test = 5;
        int b = 10;
        foo(test, &b);
    }
    
    int foo(int val, int * val2)
    {
        /* do something */
        return 0;
    }

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    prototypes end in semi-colons

    It declares the function for the compiler to look for or something like that

    It is also declares the types and the names of the variables that you will be passing into when you call it

    If you prototype a function like

    Code:
    int hi (int a, char *v);
    you can call it like
    Code:
    (a,v);
    You can change the variable name too when you are calling it
    Code:
    (b,d);
    as long as the types match.

    Then the function itself and what it does must be coded, in this example since it is int it must return a type of int

    Code:
    int hi (int a, char *v) {
    
    return 0;
    }
    Last edited by JFonseka; 05-26-2008 at 06:49 AM.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The prototype specifies what type of arguments a function takes and what it returns - nothing else (hence the compiler doesn't care which arguments you pass to the function, as long as the types match what the function expects).
    The compiler uses that information to generate code to call your function.
    Oh and make the definition and the prototype match, because the compiler will prioritize the prototype and if it compiles (usually it doesn't), then you will get bad things(TM).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  2. Function prototype questions
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 05:27 PM
  3. prototype poblem
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-30-2005, 10:01 AM
  4. Prototype syntax for sub-class constructor
    By starkhorn in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2004, 07:33 AM
  5. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM