Thread: Defining a new function in C programming.

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    Defining a new function in C programming.

    Code:
    #include<stdio.h>
    
    amount(int a, int b);
    
    int main(void)
    {
    ...
    amount(a,b)
    ...
    }
    
    amount(int a, int b)
    {
    ...
    }
    
    OR
    
    #include<stdio.h>
    
    amount(int a,int b);
    
    amount(int a, int b)
    {
    ...
    }
    
    int main(void)
    {
    ...
    amount(a,b)
    ...
    }
    So what's the difference between writing a function after main and writing a function before main?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Welcome to the forum.

    The difference is that you do need line 21, the prototype (declaration). The implementation of the function is the definition of the function.

    Check the basics of functions (check the link only if you know some basic things about pointers).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    In the second case, you do not need amount(int a,int b); (see link below)
    You have already informed the compiler of what is to come. So, when you call the function inside main, the program knows what your talking about.

    In the first case, that is called function prototype. Your telling the compiler of what is to come. Hence, when you call the function inside main, it knows what should be expected hence what it should do.

    Please also see second link. That is an incorrect way to call functions

    http://i42.tinypic.com/2hf85r5.png
    http://i41.tinypic.com/2zreu6w.png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining a function
    By Zach Sisk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2012, 09:06 AM
  2. Problem with defining function
    By curious in forum C Programming
    Replies: 17
    Last Post: 06-01-2009, 10:05 PM
  3. Help! Defining a Function
    By assaf2b in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2008, 09:07 AM
  4. Defining a custom function
    By Nalif in forum C Programming
    Replies: 3
    Last Post: 10-01-2006, 12:40 PM
  5. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM