Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Declaring a function in C

  1. #1
    Join Date
    Aug 2012
    Beans
    539

    Declaring a function in C

    Hi,
    I'm really confused about the need for declaring a function in C.

    When I write(define) a function of this type
    Code:
    int fun(int, int, ...); or
    void fun(int, int..)
    there is no need for any declaration. Direct definition would do.

    But the moment, the return type/ any of the arguments is not void/int, it gives me an error message indicating the need for declaration. For example
    Code:
    int fun(float a)
    {
     printf("\nFloat value = %f",a);
    }
    This function needs a declaration like
    Code:
    int fun(float);
    Why is this ?
    Thanks

    PS : Throughout the post, I am talking about definitions after main. If definitions were written before main, there is no need for any declarations,right. Just a disclaimer.

  2. #2
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Declaring a function

    why are you declaring anything after main()?

  3. #3
    Join Date
    Aug 2012
    Beans
    539

    Re: Declaring a function

    Didn't get you on that one.
    No, I am not declaring anything after main.

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,853
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Declaring a function

    Quote Originally Posted by IAMTubby View Post
    Why is this ?
    If only because this lets the compiler check tat you are calling functions properly and getting their results the right way?

    'Int' used to be a default in early C.

    Btw the way, if you declare a function thus:
    Code:
    int fun(float a)
    Then it should contain:
    Code:
    return some_int_value;

  5. #5
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Declaring a function

    Function prototypes are essential in a C program .

    See the "Uses" section in the below link .

    http://en.wikipedia.org/wiki/Function_prototype
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  6. #6
    Join Date
    Aug 2012
    Beans
    539

    Re: Declaring a function in C

    Okay. So can we conlude that the default return type is 'int' or 'void' and the arguments are also 'int' or 'void' ?

  7. #7
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,084
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Declaring a function in C

    Quote Originally Posted by IAMTubby View Post
    Okay. So can we conlude that the default return type is 'int' or 'void' and the arguments are also 'int' or 'void' ?
    It depends which dialect of C you are talking about. There are 3 main language definitions in use to day.

    1. Original K&R (from 1970 to 1986 approx).
    2. C89/C90 based on ANSI C 1986.
    3. C99 The current C standard.

    In K&R C, the default return type is int and parameters default to int. There is no void keyword. In C99, both implicit int and implicit function declarations are removed. C90 is somewhere in between!

    If you declare function prototypes before use, declare return types and parameter types, then you won't go far wrong whichever standard you are working to, with the caveat that void might not work with the most ancient of compilers.

    What are you using as a reference for learning C?

  8. #8
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Declaring a function in C

    Post. Your. Code.

    Declaring functions before use is always desirable but not always required.
    「明後日の夕方には帰ってるからね。」


  9. #9
    Join Date
    Aug 2012
    Beans
    539

    Re: Declaring a function in C

    Quote Originally Posted by spjackson View Post
    It depends which dialect of C you are talking about. There are 3 main language definitions in use to day.

    1. Original K&R (from 1970 to 1986 approx).
    2. C89/C90 based on ANSI C 1986.
    3. C99 The current C standard.

    In K&R C, the default return type is int and parameters default to int. There is no void keyword. In C99, both implicit int and implicit function declarations are removed. C90 is somewhere in between!

    If you declare function prototypes before use, declare return types and parameter types, then you won't go far wrong whichever standard you are working to, with the caveat that void might not work with the most ancient of compilers.

    What are you using as a reference for learning C?
    Hmm, not really using a fixed reference. sorry!
    But I'm using gcc as my compiler ? Is that enough information to figure out which C I'm using ?

  10. #10
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Declaring a function in C

    lol he really doesn't want to post his code does he?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •