Thread: Function Question

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    Function Question

    Hi im just wondering if there is a differnce between

    Code:
    #include <stdio.h>
    
    int twice(int i);
    
    int main(void)
    {
        int input;
    
        printf("Enter number to be doubled: ");
        scanf("%d", &input);
    
        printf("%d X 2 = %d\n", input, twice(input));
    }
    
    int twice(int i)
    {
        return (i * 2);
    }
    and

    Code:
    #include <stdio.h>
    
    int twice(int i)
    {
        return (i * 2);
    }
    
    int main(void)
    {
        int input;
    
        printf("Enter number to be doubled: ");
        scanf("%d", &input);
    
        printf("%d X 2 = %d\n", input, twice(input));
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    no difference whatsoever, other than one uses a prototype because the function twice() isn't defined until after it is called. Writing it either way is correct.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Writing it either way is correct.
    But I always put a prototype in. Then you can move the function to the end of the file or another file altogether and it still compiles.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    22
    The main difference is that in second case you dont need to decleare the function but in first case you have to.

    This is because program starts compiling from top to bottom.so in first case when it found function declearation then it(compiler) makes entry for it in its funtion table.and when it is called then it will move up to defination part that is at last in your example.

    while in second case you have make function defination before main() which will save your work that is function defination eliminates the need of its delclaration.

    so doing both is good as well but most of people use the first way to make there program to give a clearer understanding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM