You are here:

C++/default arguments

Advertisement


Question
HI
i have a question about default arguments in c++.
could you plz tell me what's the implication of"Default arguments must be the rightmost (trailing) arguments in a function's parameter list"
actually i don't know about rightmost(trailling) arguments.

thanx
Bita

Answer
Hi, Bita.

All this really means is that when you have default arguments, the parameters they apply to must be the last parameters in the function.  Take the following function:

   int doSomething(int input1, int input2, int input3);

To call this function, you would do something like the following:

   int result = doSomething(param1, param2, param3);

Now, let's assume that the doSomething function allows for a default argument on a parameter.  In order to do this, you must place the parameter with a default argument at the end of the parameter list, like so:

   int doSomething(int input1, int input2, int input3 = 0);

Now, you can call the function in one of two ways:

   int result1 = doSomething(param1, param2, param3);
   int result2 = doSomething(param1, param2);

If you have multiple parameters with default arguments, they must all be after the parameters without default arguments.  Using the above function as an example again:

   int doSomething(int input1, int input2 = 0, int input3 = 0);

So, given all of this, the following are illegal in C++:

   int doSomething(int input1 = 0, int input2, int input3);
   int doSomething(int input1, int input2 = 0, int input3);

I believe the primary reason for this is that it makes parsing function calls considerably more difficult.  I suppose it would have been feasible for the creators of C++ to implement a method for users to have default parameters scattered about, but it's much easier to have a clearly defined method that's easy to parse, such as requiring that all default parameters be placed at the end of the parameter list.

If I have left you with any questions or perhaps raised new questions, please do not hesitate to follow up with me.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.