Thread: Function name basic question help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    Post Function name basic question help

    The best thing to use a function is first to declare it before you even get into the main(), which is the case where all the examples in the tutorial book do. This is in my case.

    However, here is the question. I am quite confused with the name or variable of the function as well as the argument_list.

    For example, in the book here is the example of it.

    Code:
    // Function must be declared before being used.
    
    (I have omitted some of the lines or codes, and this is how it is declared)
    
    int triangle(int num); --> this is the declaration of the function and int num is the argument 
    
    main(){
    
    (this area is omitted)
    
    }
    
    // Triangle number function
    
    int triangle(int n){ --> This is how the function is being defined
    
    (this area is omitted)
    
    }
    My question is whether the argument (int num) in the declaration of the function has to be the same as the argument (int n) in the defined function? Or can be they different or I could use any name for them? Because sometimes when I browsed through the book, in some examples, they are just the same but sometimes a bit different and as I am new to function, it would be quite a bit of confusing to me. I am being baffled.

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    It depends how you declare the function. If you were to declare the function

    Code:
    int triangle(int num);
    Then in the function definition you must use the variable name "num" like so:

    Code:
    int triangle(int num) { 
       code 
    }
    Where as if you were to declare it like so:

    Code:
    int triangle(int);
    Then you could define it in a way that it uses any variable name you wish. Such as:

    Code:
    int triangle(int abc) {
       code
    }
    Both ways work, but it's just better form to use the variable name that you use in the definition.
    Last edited by pobri19; 09-23-2008 at 11:55 PM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    well...I have read some other tutorials, the function call could also include parameters that are quite different from the parameters of the function definition. I think I gotta give you guys the examples from the book. It is still confusing.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Pobri is wrong unfortunately. The names the function prototype uses for parameters and the names the function definition use do not need to be the same. The function name, return type, type and order of the parameters should be the same.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by citizen View Post
    Pobri is wrong unfortunately. The names the function prototype uses for parameters and the names the function definition use do not need to be the same. The function name, return type, type and order of the parameters should be the same.
    That's what I said ;p
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pobri19 View Post
    That's what I said ;p
    That is not clear from this:

    Code:
    int triangle(int num);
    Then in the function definition you must use the variable name "num" like so:

    Code:
    int triangle(int num) { 
       code 
    }
    It is also perfectly valid to use
    Code:
    int triangle(int abc) { 
       code 
    }
    It is not good style to change the names, but it's perfectly legal from the compilers perspective.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And it's not good style to omit the names of the variables in the declarations either.
    Keep both declaration and definition the same to avoid confusion and trouble.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Thanks so much! It helps a lot!

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. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM