Thread: function prototypes question.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    function prototypes question.

    what good are function prototypes for? in the book that in reading they have an example where the code below appears on line 3
    Code:
     
    int Area(int Length, int Width); //function prototype
    on line 27 the function appears as
    Code:
    int Area(int L, int W)
    {
        return L * W;
    }
    so the protoype is not the same, so whats the purpose of having it there if it doesnt do anything?

    im new so silly questions r a must!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something between line 3 and line 27 could call Area()
    WIthout a prototype, that would be an error.

    Just look in any header file you include, those are all prototypes.

    You can only write trivial programs which can be expressed
    a) in a single file
    b) with no mutual recursion
    without the need to write a prototype.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    still a bit confused but i think i understand what u mean, i have another question
    what does this mean
    Code:
    void MakeArray(ArrayType NumArray, int & count)
    im talking about the & sign between int and count

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >im talking about the & sign between int and count
    It means that count is a reference to int. You can safely assume that any changes to count will have an effect on an integer from the calling function.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in the prototype, the names of the variables are immaterial, in fact they aren't even necessary. The argruments in the pototype only needs to have the class type indicated. The class type need to be the same, in the same order in the prototype and the first line of the definition but the variable names can be different. The function body will use the names of the variables as listed in the first line of the function definition, irrespective of the name of variables in the prototype and irrespective of the names of the variables passed to the function in the function call.

    Code:
    //prototype
    int add(int a, int b);
     
    //function call
    int first = 3;
    int second = 1;
    int sum = add(first, second)
     
    //definition
    int add(int lhs, int rhs)
    {
       return lhs + rhs;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. function pointer question
    By andrea72 in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 06:25 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM