When passing optional arguments, which parameter gets chosen?

Hi!

If I have a function:

void myFunc(int arg1, int arg2=1, int arg3=1);

And I only pass it one variable, myFunc(8), it will choose 8 to be arg1, then supply 1 for both arg2 and arg3, right?

But what if I supply two variables? Which does it choose? For example if I do:
myFunc(8,3), is arg2 or arg3 equal to 3? Can I even legally do this?

Thanks!
That is perfectly legal. You can even do
myFunc(,,12);

EDIT: WHOOPS! You can't actually use the piece of code I listed above, due to the fact that there is no default for the first argument. I didn't notice that detail before posting. Sorry.

Your reasoning for the first question's answer is correct.

For your second question's answer, it will set arg1 to 8, arg2 to 3, and because you supplied nothing for arg3, will set arg3 to its default of 1. It's quite systematic.

Further explanation:
http://cplusplus.com/doc/tutorial/functions2/

-Albatross
Last edited on
Ah, thanks!
That is perfectly legal. You can even do
myFunc(,,12);
What? No you can't. Why would you say that?

Arguments are filled left to right when calling functions.
Default arguments must be contiguously adjacent and aligned to the right-end of the parameter list.

As per your example, if you supply myFunc(8,3), then arg1=8 and arg2=3, and arg3 defaults to 1.
ARGH! That was a stupid mistake.

Sorry, you can't do myFunc(,,12);, because there's no default for the first argument. My bad, didn't notice that. Sorry.

-Albatross
Last edited on
It seems to me that it was possible in another, interpreted language, but I don't remember which one.

[edit] Oh, it was BASIC, wasn't it? I think I've used that construct in BASIC programs...
Last edited on
Wait...so could I do myFunc(, , 12); if all three parameters had a default argument?

Also, in my original example, is there a way to make arg1=8, arg2 be the default, and arg3=3 without just passing the default to arg2? Could I do myFunc(8, , 3)?
After doing some digging around, it seems that you can't actually do any of that in C++. I am terribly sorry, but it seems that I got my languages mixed up. In C++, you can omit arguments like myFunc(8,2) or myFunc(12) and it will be perfectly valid, but you cannot under any circumstances SKIP arguments.

Duoas also mentioned this in his post above his above post.

-Albatross
Last edited on
Of course, there is always the Named Parameter Idiom, for extra fun. :-)

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

class myFunc
  {
  private:
    int _a, _b, _c;

  public:
    myFunc( int a = 1, int b = 2, int c = 3 ): _a( a ), _b( b ), _c( c ) { }
    inline myFunc& a( int value ) { _a = value; return *this; }
    inline myFunc& b( int value ) { _b = value; return *this; }
    inline myFunc& c( int value ) { _c = value; return *this; }
    operator int ()
      {
      return (_a * _b) + _c;
      }
  };

int main()
  {
  cout << myFunc()               << endl;  // prints (1 *  2) + 3 --> 5
  cout << myFunc().b( 10 )       << endl;  // prints (1 * 10) + 3 --> 13
  cout << myFunc( 5, 7 )         << endl;  // prints (5 *  7) + 3 --> 38
  cout << myFunc().a( 5 ).b( 7 ) << endl;  // same as last

  return 0;
  }

:-]
Aw... that's cheating. ;)

-Albatross
Topic archived. No new replies allowed.