Prototypes?

This is a discussion on Prototypes? within the C++ Programming forums, part of the General Programming Boards category; http://www.cppreference.com/stdstring/strstr.html Code: #include <cstring> char *strstr( const char *str1, const char *str2 ); http://www.opengroup.org/onlinepubs/...ns/strstr.html Code: #include <string.h> char *strstr(const char ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    http://www.cppreference.com/stdstring/strstr.html

    Code:
      #include <cstring>
      char *strstr( const char *str1, const char *str2 );
    http://www.opengroup.org/onlinepubs/...ns/strstr.html

    Code:
    #include <string.h>
    
    char *strstr(const char *s1, const char *s2);
    I don't know. You tell me.

    As you can tell, they didn't choose very meaningful names, but regardless of variable names, shouldn't documentation be consulted when using a function?

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Not really. The entire purpose of having meaningful names is to make it unnecessary to consult the documentation every time you use the function. What I remember about strstr is "searches for a substring". The char* return type tells me that it returns a pointer to the found position. I wish the argument names would tell me where I put the needle and where the haystack.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    I don't know. You tell me.
    hmm... I get your point. The second prototype is identical to the version from C99.

    On the other hand, C++03's version of std::find is:
    Code:
    template<class InputIterator, class T>
    InputIterator find(InputIterator first, InputIterator last, const T& value);
    It is a non-member function template, and so analogous to strstr(). Personally, I find that the C++ version is clearer: I get a hint on what are the parameters without having to look further into the text of the standard. With the C99 version, I must look further into the text of the standard. If you do not know what they do, then either way you have to read further, but if you only want to know the order of arguments, having descriptive parameter names looks good to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    They do help. You may know a lot of functions and what they do, but sometimes you can forget what the parameters are and what they do. It's very easy to mix up source & destination and arguments that are like each other, for example.
    All I see is bad practice for non-descriptive prototypes.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If the arguments have sensible names, then having the variable names there is a help. If they are just meaningless names (such as s1, s2 in the strstr() example above), then it makes little difference. Of course, if you use the method I do, which is to write the function first, then copy it to the prototype location, it's meaningless to go and edit the function prototype to REMOVE the names - but that's depending on how you go about producing the prototype in the first place.

    --
    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.

  6. #21
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Wow, this site is great.

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Thank you. Does it always have to be double, because those are the only examples i seem to find?

    -After the calcint(amount, rate), do i have to put in parentheses which things are associated with the function prototype?

  8. #23
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by vart
    not a good practice remove var names from function prototypes - makes code harder to read
    true, it's a lazy-ish practice

  9. #24
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by MacGyver View Post
    I don't know. You tell me.
    http://msdn2.microsoft.com/en-us/lib...kz(VS.80).aspx

    Code:
    char *strstr(
       const char *str,
       const char *strSearch 
    ); // C only
    first is a string, second is a substring. And it is shown by intellisense - I do not need to go to the external documentation if I know in general what function does (In VS2008 it is even clearer - _Str and _SubStr)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Nothing on MSDN is ever clear.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function prototypes - needed yes or no?
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 07:39 AM
  2. Class prototypes
    By m00se123 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 02:06 AM
  3. Is there a difference between these prototypes?
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2002, 10:28 AM
  4. function prototypes and call statements.
    By mutu in forum C Programming
    Replies: 0
    Last Post: 04-04-2002, 11:39 PM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21