CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2019
    Posts
    3

    Functions with Default Arguments in C++ and C

    Hello Everyone, I want to know head to head comparison between the use of functions with default arguments in C++ and C. I am confused which is better work for function with default arguments. Can anyone know about it? As my knowledge C doesn’t. If such a function is called by passing argument(s) then those argument(s) are used by the function. Can anyone suggest me proper comparison?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Functions with Default Arguments in C++ and C

    In C++, arguments from the right to to left can be provided with default values to be used by the function if the caller doesn't provide a specified parameter. These are most often used for arguments where there is typically a 'most used' value (default) with a specific argument only needing to be passed when this default is not appropriate. Working from the right, arguments can have a default specified until an argument isn't specified with a default. Then all the remaining arguments to the left can't have a default specified value. All arguments are used by the function in the same way - whether the value is the default value or the passed value.

    A simple example:

    Code:
    void testfun(const std::string& str, char delim = '\n')
    {...}
    
    ...
    
    testfun("abcde");         // Uses '\n' as delim
    testfun("zxcvb", ',');    // Uses , as delim
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: Functions with Default Arguments in C++ and C

    Quote Originally Posted by ankitdixit View Post
    Can anyone suggest me proper comparison?
    There is no need to resort to C to find guidance about C++ usage. C++ is a language in its own right with its own guidelines. See for example,

    F.51: Where there is a choice, prefer default arguments over overloading
    C.140: Do not provide different default arguments for a virtual function and an overrider

    in the C++ Core Guidelines.

    https://github.com/isocpp/CppCoreGui...eGuidelines.md
    Last edited by wolle; April 17th, 2020 at 12:09 AM.

  4. #4
    Join Date
    Mar 2019
    Posts
    3

    Re: Functions with Default Arguments in C++ and C

    Thanks to solving my problem, Now I am clear it to while C++ allows using a function with default arguments, C doesn’t. If such a function is called by passing argument(s) then those argument(s) are used by the function.

    [Mod. Link to site removed so post not considered spam]
    Last edited by 2kaud; April 24th, 2020 at 07:06 AM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured