1

I am new in c++ i have a question on default argument. If there a function with a following prototype

void f(int=10,int=20,int=30,int=40)

If this funcion is called by passing 2 arguments to it,how can we make sure that these argumnts are treated as first and third whereas, the second and forth are taken as defaults.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
arpit
  • 555
  • 1
  • 7
  • 25

7 Answers7

7

You can't. Arguments to functions match the parameters in order. You can use overloading instead of default arguments like this:

void myFunc(int a,int b,int c,int d);
void myFunc(int a,int c) {
    myFunc(a,20,c,40);
}
IronMensan
  • 6,761
  • 1
  • 26
  • 35
  • 1
    How would you make sure that `myFunc(1,2)` calls the second function, not the first? Also, what if you wanted to call the first function, passing first two arguments? – Nawaz Nov 18 '11 at 05:39
  • If you don't define default values in the prototype of `void myFunc(int a,int b,int c,int d)` compiler will chose proper function. Otherwise it will give you compiler error. – expert Nov 18 '11 at 05:42
  • 3
    @Nawaz I didn't put default arguments on any of the parameters. If you want the first one, provide four arguments or two for the second. – IronMensan Nov 18 '11 at 05:46
  • @IronMensan: Exactly. Ypu've changed the behaviour. What if I write `myFunc(1,2)` where the arguments are first two arguments? – Nawaz Nov 18 '11 at 05:47
  • 1
    @Nawaz you can't have it both ways, myFunc with two arguments has to mean something and the compiler can't read your mind to figure out what. The OP asked for something that can't be done and I suggested something else that might meet his needs. – IronMensan Nov 18 '11 at 05:54
  • @IronMensan: In that case, I think, the order of parameters should be changed in the original declaration. Third parameter should become second, and second parameter should become third. Overload is not needed! – Nawaz Nov 18 '11 at 05:55
  • @Nawaz that might be a better solution but there's no way to know without knowing more about the existing codebase and how much the function is used. – IronMensan Nov 18 '11 at 06:02
3

Shouldn't be possible. They'd be treated as the first two.

You can just create a function with a different name, taking two arguments and calling f.

Alternatively, if you want to emulate named arguments, you can use something similar to fluent interfaces. Example:

#include <iostream>
using namespace std;

int f_impl(int a,int b, int c, int d){
    cout << a << " " << b << " " << c << " " << d << endl;
    return 42;
}

struct f{
    int _a, _b, _c, _d;
    f() : _a(10), _b(20), _c(30), _d(40){}
    f& a(int a){ _a = a; return *this;}
    f& b(int b){ _b = b; return *this;}
    f& c(int c){ _c = c; return *this;}
    f& d(int d){ _d = d; return *this;}
    int operator()(){ return f_impl(_a, _b, _c, _d); }
};

#define F(x) (f()x())

int main(){

    f().a(100).c(300)();

    cout << F(.b(1000).d(4000)) << endl;

    return 0;
}

Output:

100 20 300 40
10 1000 30 4000
42
Vlad
  • 18,195
  • 4
  • 41
  • 71
3

That's not how default arguments in C++ work. If you pass two arguments to function f, they will always stand in as the first two arguments, while the last two would be 30 and 40.

In other words, C++ functions only support positional parameters.

wkl
  • 77,184
  • 16
  • 165
  • 176
1

As other people said you can't do that in C++.

But you can create struct/class with four integer members that initialized to values you defined. And you will pass it as a parameter to the function.

Example

struct Param
{
  int a;
  int b;
  int c;
  int d;

  Param() : a(10), b(20), c(30), d(40) {}
  void setA(int value) { a = value; }
  void setB(int value) { a = value; }
  void setC(int value) { a = value; }
  void setD(int value) { a = value; }
}

void f(Param& param) {}

Param param;
param.setA(67);
param.setC(9);
f(param);
expert
  • 29,290
  • 30
  • 110
  • 214
1

The feature you are asking for is called 'Named Parameters'.

Default parameters and Named parameters combined give you lot more options to do things like you suggested but unforunately, C++ doesn't have named parameters. However, some other languages like C# and VB and probably Python have named parameters

Aamir
  • 14,882
  • 6
  • 45
  • 69
0

Default arguments will be assigned from last if necessary.

http://en.wikipedia.org/wiki/Default_argument

Raja
  • 509
  • 2
  • 10
  • 20
0

Actually you can have named parameters in C++ with a little help from boost::parameter ( also mentioned in the wiki article on named parameters ).

Ylisar
  • 4,293
  • 21
  • 27