Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Optional arguments

Optional arguments

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
15 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SWDevil

    Hi, I have a function that is declared as follows: void func(int A, int B, int C, int D=-1, int E=-1); arguments D and E are optional, and so if the function is called like this: func(A,B,C); then D and E recieve the value -1. What I want to do is add another optional argument, let's say int F that will also receive -1 as default, for example. The problem is that sometimes I want to call the function only with the parameters A,B,C and F. If I declare the function like this: void func(int A, int B, int C, int D=-1, int E=-1, F); will I be able to call the function in this manner: func(A,B,C,,,F); ?

    _ Offline
    _ Offline
    _anil_
    wrote on last edited by
    #3

    SWDevil wrote:

    void func(int A, int B, int C, int D=-1, int E=-1, F);

    forget about calling the funtion, C++ won't allow to declare the funtion like this. If you declare an argumnet with default value then all the arguments after that should have default value . Regards Anil

    S 1 Reply Last reply
    0
    • _ _anil_

      SWDevil wrote:

      void func(int A, int B, int C, int D=-1, int E=-1, F);

      forget about calling the funtion, C++ won't allow to declare the funtion like this. If you declare an argumnet with default value then all the arguments after that should have default value . Regards Anil

      S Offline
      S Offline
      SWDevil
      wrote on last edited by
      #4

      oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

      C O _ 3 Replies Last reply
      0
      • S SWDevil

        oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #5

        Nope. You have to provide the arguments in the same order. Whenever you supply a value for an optional parameter, it will be used for the first optional parameter.


        Cédric Moonen Software developer
        Charting control

        1 Reply Last reply
        0
        • S SWDevil

          oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

          O Offline
          O Offline
          Owner drawn
          wrote on last edited by
          #6

          SWDevil wrote:

          but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

          Well if you are desperate to get it working take a look at va_arg, va_list, va_start, va_end. It's an option.


          Owner drawn Jesus Loves

          T R 2 Replies Last reply
          0
          • S SWDevil

            oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

            _ Offline
            _ Offline
            _anil_
            wrote on last edited by
            #7

            No you can't :-( What you want is calling the function void func(int A, int B, int C, int D=-1, int E=-1, F=-1); in func(A,B,C,,,F); Suppose( which will never happen)if you succeed, you will get D and E as -1. So you can call func(A, B, C, -1, -1, F); :-) Regards Anil

            1 Reply Last reply
            0
            • S SWDevil

              Hi, I have a function that is declared as follows: void func(int A, int B, int C, int D=-1, int E=-1); arguments D and E are optional, and so if the function is called like this: func(A,B,C); then D and E recieve the value -1. What I want to do is add another optional argument, let's say int F that will also receive -1 as default, for example. The problem is that sometimes I want to call the function only with the parameters A,B,C and F. If I declare the function like this: void func(int A, int B, int C, int D=-1, int E=-1, F); will I be able to call the function in this manner: func(A,B,C,,,F); ?

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #8

              why not putting the F parameter between C and D ?

              void foo(int A, int B, int C, int F = -1, int D = -1, int E = -1) {
              //...
              }

              so that you can use it like this :

              int A, B, C, D, E, F;

              foo(A, B, C); // A B C -1 -1 -1
              foo(A, B, C, F); // A B C -1 -1 F
              foo(A, B, C, F, D); // A B C D -1 F
              foo(A, B, C, F, D, E); // A B C D E F


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              S 1 Reply Last reply
              0
              • T toxcct

                why not putting the F parameter between C and D ?

                void foo(int A, int B, int C, int F = -1, int D = -1, int E = -1) {
                //...
                }

                so that you can use it like this :

                int A, B, C, D, E, F;

                foo(A, B, C); // A B C -1 -1 -1
                foo(A, B, C, F); // A B C -1 -1 F
                foo(A, B, C, F, D); // A B C D -1 F
                foo(A, B, C, F, D, E); // A B C D E F


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                S Offline
                S Offline
                SWDevil
                wrote on last edited by
                #9

                But I also sometimes want to call the function without the F parameter (and with the D and E parameters)... :(

                T T 2 Replies Last reply
                0
                • S SWDevil

                  But I also sometimes want to call the function without the F parameter (and with the D and E parameters)... :(

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #10

                  so write several overloads of the function...!


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  1 Reply Last reply
                  0
                  • S SWDevil

                    Hi, I have a function that is declared as follows: void func(int A, int B, int C, int D=-1, int E=-1); arguments D and E are optional, and so if the function is called like this: func(A,B,C); then D and E recieve the value -1. What I want to do is add another optional argument, let's say int F that will also receive -1 as default, for example. The problem is that sometimes I want to call the function only with the parameters A,B,C and F. If I declare the function like this: void func(int A, int B, int C, int D=-1, int E=-1, F); will I be able to call the function in this manner: func(A,B,C,,,F); ?

                    B Offline
                    B Offline
                    BadKarma
                    wrote on last edited by
                    #11

                    Hi, the simples solution is to provide a wrapper macro for your function

                    void func(int A, int B, int C, int D=-1, int E=-1, int F=-1);
                    #define FUNC(a_, b_, c_, f_) func(a_, b_, c_, -1, -1, f_)
                    

                    This way you can simply call it through the macro in those rare occasions where only the F should be given.

                    void Call()
                    {
                      int A, B, C, D, E, F;
                    
                      // normal call
                      func(A, B, C);
                      func(A, B, C, D, E, F);
                    
                      // special call
                      FUNC(A, B, C, F);
                    }
                    

                    codito ergo sum

                    1 Reply Last reply
                    0
                    • O Owner drawn

                      SWDevil wrote:

                      but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

                      Well if you are desperate to get it working take a look at va_arg, va_list, va_start, va_end. It's an option.


                      Owner drawn Jesus Loves

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #12

                      Owner drawn wrote:

                      Well if you are desperate to get it working take a look at va_arg, va_list, va_start, va_end. It's an option.

                      Well Back after long time:)

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                      1 Reply Last reply
                      0
                      • S SWDevil

                        But I also sometimes want to call the function without the F parameter (and with the D and E parameters)... :(

                        T Offline
                        T Offline
                        ThatsAlok
                        wrote on last edited by
                        #13

                        SWDevil wrote:

                        ut I also sometimes want to call the function without the F parameter (and with the D and E parameters)..

                        What about using Variable Argument System!

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                        1 Reply Last reply
                        0
                        • O Owner drawn

                          SWDevil wrote:

                          but if I declare it this way, will I be able to call the func without specifying the parameters D and E?

                          Well if you are desperate to get it working take a look at va_arg, va_list, va_start, va_end. It's an option.


                          Owner drawn Jesus Loves

                          R Offline
                          R Offline
                          Rajesh R Subramanian
                          wrote on last edited by
                          #14

                          Where are you buddy? These days you are not posting messages to the board.

                          Nobody can give you wiser advice than yourself. - Cicero

                          O 1 Reply Last reply
                          0
                          • R Rajesh R Subramanian

                            Where are you buddy? These days you are not posting messages to the board.

                            Nobody can give you wiser advice than yourself. - Cicero

                            O Offline
                            O Offline
                            Owner drawn
                            wrote on last edited by
                            #15

                            brahmma wrote:

                            Where are you buddy? These days you are not posting messages to the board.

                            Hmm.. I was away for quite a while but now alive and kicking. ;)


                            Owner drawn Jesus Loves

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups