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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Another academic question - ANSI C standard on optional function parameters

Another academic question - ANSI C standard on optional function parameters

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
14 Posts 4 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.
  • CPalliniC CPallini

    ANSI C only provides variable number of arguments, via the va_list, va_start, va_arg, va_end mechanism. See, for instance here[^].

    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

    V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #4

    Thanks, but I may have messed up my question. What I was looking for - is this syntax ANSI C standard function(int a, int b = 0) Where passing the second parameter in function call is optional. Cheers Vaclav

    L 1 Reply Last reply
    0
    • V Vaclav_

      Thanks, but I may have messed up my question. What I was looking for - is this syntax ANSI C standard function(int a, int b = 0) Where passing the second parameter in function call is optional. Cheers Vaclav

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      No, that's C++; there is no overloading in C. See http://msdn.microsoft.com/en-us/library/cts394c0.aspx[^].

      V 1 Reply Last reply
      0
      • L Lost User

        No, that's C++; there is no overloading in C. See http://msdn.microsoft.com/en-us/library/cts394c0.aspx[^].

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #6

        Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav

        CPalliniC L 2 Replies Last reply
        0
        • V Vaclav_

          Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #7

          Vaclav_Sal wrote:

          I thought   overloading was having same function name with different types of variables.
          Such as
          function(int)
          function(void*)
          function(double)

          You thought right.

          Vaclav_Sal wrote:

          That works fine in Arduino C++ compiler

          As it should: after all is a C++ compiler. You have to understand that C and C++ are two different programming languages. While C++ supports function overloading, C does not.

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • V Vaclav_

            Now I am totally lost. I thought overloading was having same function name with different types of variables. Such as function(int) function(void*) function(double) That works fine in Arduino C++ compiler.( And right now I have a bug in my code because I am using it). I am assuming Arduino compiler is C++, did not really try to find out. But since it works with classes....it must be C++ based. Cheers Vaclav

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            The following defintion is implied overloading since you can call it in one of two ways.

            function(int a, int b = 0);

            //
            function(1, 2); // two parameters with explicit values

            //
            function(1); // one explicit and one implicit (0) parameter

            V 1 Reply Last reply
            0
            • V Vaclav_

              What is a current C ( ANSI C) standard on putting optional parameters / attributes into function? Or is there such a thing - ANSI standard _ anymore? "Popular" microprocessor compiler does not allow such thing and "workaround" is function overloading. Which IMHO in memory limited device adding more similar functions into code does not make much sense, unless you are memory chips salesman. Cheers Vaclav

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

              Since C doesn't allow overloading to start with, the only way to call a function with alternate sets of parameters is by thinking up a pattern on how you pass these parameters in a less rigid way. One way has already been mentioned: passing an array of void pointers. The disadvantage is that you have to figure out what is what, including type, and if more than one parameter is optional you need to additionally pass some information about which of the parameters were skipped. Pretty much the same goes for vararg lists. A slightly better approach that offers strict control over types and meaning of parameters is passing a struct that contains all possible parameters: write one function that allocates and/or initializes the struct with defaults, and in the code overwrite all values that you want different, then pass the struct to your processing function. If you have mandatory parameters without sensible defaults (e. g. array sizes), make sure to pass these to the intialization function.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              1 Reply Last reply
              0
              • L Lost User

                The following defintion is implied overloading since you can call it in one of two ways.

                function(int a, int b = 0);

                //
                function(1, 2); // two parameters with explicit values

                //
                function(1); // one explicit and one implicit (0) parameter

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #10

                OK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav

                L S 2 Replies Last reply
                0
                • V Vaclav_

                  OK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  That does not make any sense at all, you are using two different functions.

                  V 1 Reply Last reply
                  0
                  • V Vaclav_

                    OK, I did try this void TestFunctionA (int a, int b = 100) { a += 1; b += 10; Serial.print(b); } void TestFunctionB (int a) { a += 1; // b += 10; Serial.print(a); } and calls TestFunctionB (10); // int a, int b = 100) TestFunctionA(10,234); And the compiler said too many arguments in function call But if I change the b to float it compiles and it does makes sense to me. Partially. If the b parameter b is not passed, than the function A will be executed and compiled. Confusing, yes. But that was not the reason I asked. In "real" C compiler when a parameter is declared optional the code will compile without having to change all function calls to include the optional parameter. But I still do not know if that is ANY standard or just MS compiler specific. But Arduino compiler expects the "optional" parameter, so it is really not optional. Cheers Vaclav

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #12

                    This doesn't compile as C code in VisualStudio 2010. If it did for you, you compiled as C++. Hint: in VS 2010 you can't create a C project directly, only a C/C++ project that will contain *.cpp files which by default will be compiled as C++. You can override that behaviour by explicitely forcing C compilation in the Compiler->Advanced settings tab of the project. Or you can put your C code in a *.C file (that you will have to explicitely create, of course).

                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                    1 Reply Last reply
                    0
                    • L Lost User

                      That does not make any sense at all, you are using two different functions.

                      V Offline
                      V Offline
                      Vaclav_
                      wrote on last edited by
                      #13

                      Whoops I was trying to demo a point and messed it up adding the A and B to the functions.

                      L 1 Reply Last reply
                      0
                      • V Vaclav_

                        Whoops I was trying to demo a point and messed it up adding the A and B to the functions.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        It is always best to write and test your code, and then use copy and paste to show exactly what the issue may be.

                        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